diff -r 52fbc7bb78ad Modules/_winapi.c --- a/Modules/_winapi.c Sat Jan 11 00:16:50 2014 +0100 +++ b/Modules/_winapi.c Sun Jan 12 22:26:36 2014 -0600 @@ -57,13 +57,36 @@ #define F_HANDLE F_POINTER #define F_DWORD "k" -#define F_BOOL "i" -#define F_UINT "I" #define T_HANDLE T_POINTER #define DWORD_MAX 4294967295U +/*[clinic input] +module _winapi +class _winapi.Overlapped +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + +/*[python input] +def create_converter(type_, format_unit): + name = type_ + '_converter' + # registered upon creation by CConverter's metaclass + type(name, (CConverter,), {'type': type_, 'format_unit': format_unit}) + +# format unit differs between platforms for these +create_converter('HANDLE', '" F_HANDLE "') +create_converter('HMODULE', '" F_HANDLE "') +create_converter('LPSECURITY_ATTRIBUTES', '" F_POINTER "') + +create_converter('BOOL', 'i') # F_BOOL used previously (always 'i') +create_converter('DWORD', 'k') # F_DWORD is always "k" (which is much shorter) +create_converter('LPCTSTR', 's') +create_converter('UINT', 'I') # F_UINT used previously (always 'I') + +[python start generated code]*/ +/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + /* Grab CancelIoEx dynamically from kernel32 */ static int has_CancelIoEx = -1; static BOOL (CALLBACK *Py_CancelIoEx)(HANDLE, LPOVERLAPPED); @@ -146,8 +169,23 @@ PyObject_Del(self); } +/*[clinic input] +_winapi.Overlapped.GetOverlappedResult + + self: self(type='OverlappedObject *') + waitobj: object + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_Overlapped_GetOverlappedResult__doc__, +"GetOverlappedResult(waitobj)"); + +#define _WINAPI_OVERLAPPED_GETOVERLAPPEDRESULT_METHODDEF \ + {"GetOverlappedResult", (PyCFunction)_winapi_Overlapped_GetOverlappedResult, METH_O, _winapi_Overlapped_GetOverlappedResult__doc__}, + static PyObject * -overlapped_GetOverlappedResult(OverlappedObject *self, PyObject *waitobj) +_winapi_Overlapped_GetOverlappedResult(OverlappedObject *self, PyObject *waitobj) +/*[clinic end generated code: checksum=4cdaac7be9d998fb937998e4bb4ce6f7918afbd4]*/ { int wait; BOOL res; @@ -185,8 +223,34 @@ return Py_BuildValue("II", (unsigned) transferred, (unsigned) err); } +/*[clinic input] +_winapi.Overlapped.getbuffer + + self: self(type='OverlappedObject *') +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_Overlapped_getbuffer__doc__, +"getbuffer()"); + +#define _WINAPI_OVERLAPPED_GETBUFFER_METHODDEF \ + {"getbuffer", (PyCFunction)_winapi_Overlapped_getbuffer, METH_NOARGS, _winapi_Overlapped_getbuffer__doc__}, + static PyObject * -overlapped_getbuffer(OverlappedObject *self) +_winapi_Overlapped_getbuffer_impl(OverlappedObject *self); + +static PyObject * +_winapi_Overlapped_getbuffer(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = _winapi_Overlapped_getbuffer_impl((OverlappedObject *)self); + + return return_value; +} + +static PyObject * +_winapi_Overlapped_getbuffer_impl(OverlappedObject *self) +/*[clinic end generated code: checksum=82c35b00a0f763493ce80b5a8f8724251ff6cf58]*/ { PyObject *res; if (!self->completed) { @@ -200,8 +264,34 @@ return res; } +/*[clinic input] +_winapi.Overlapped.cancel + + self: self(type='OverlappedObject *') +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_Overlapped_cancel__doc__, +"cancel()"); + +#define _WINAPI_OVERLAPPED_CANCEL_METHODDEF \ + {"cancel", (PyCFunction)_winapi_Overlapped_cancel, METH_NOARGS, _winapi_Overlapped_cancel__doc__}, + static PyObject * -overlapped_cancel(OverlappedObject *self) +_winapi_Overlapped_cancel_impl(OverlappedObject *self); + +static PyObject * +_winapi_Overlapped_cancel(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = _winapi_Overlapped_cancel_impl((OverlappedObject *)self); + + return return_value; +} + +static PyObject * +_winapi_Overlapped_cancel_impl(OverlappedObject *self) +/*[clinic end generated code: checksum=7be400d81eaabd1726ba547b892bea9af3916a5f]*/ { BOOL res = TRUE; @@ -222,10 +312,9 @@ } static PyMethodDef overlapped_methods[] = { - {"GetOverlappedResult", (PyCFunction) overlapped_GetOverlappedResult, - METH_O, NULL}, - {"getbuffer", (PyCFunction) overlapped_getbuffer, METH_NOARGS, NULL}, - {"cancel", (PyCFunction) overlapped_cancel, METH_NOARGS, NULL}, + _WINAPI_OVERLAPPED_GETOVERLAPPEDRESULT_METHODDEF + _WINAPI_OVERLAPPED_GETBUFFER_METHODDEF + _WINAPI_OVERLAPPED_CANCEL_METHODDEF {NULL} }; @@ -299,22 +388,49 @@ /* -------------------------------------------------------------------- */ /* windows API functions */ -PyDoc_STRVAR(CloseHandle_doc, -"CloseHandle(handle) -> None\n\ -\n\ -Close handle."); +/*[clinic input] +_winapi.CloseHandle + + handle: HANDLE + / + +Close handle. +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_CloseHandle__doc__, +"CloseHandle(handle)\n" +"Close handle."); + +#define _WINAPI_CLOSEHANDLE_METHODDEF \ + {"CloseHandle", (PyCFunction)_winapi_CloseHandle, METH_VARARGS, _winapi_CloseHandle__doc__}, static PyObject * -winapi_CloseHandle(PyObject *self, PyObject *args) +_winapi_CloseHandle_impl(PyModuleDef *module, HANDLE handle); + +static PyObject * +_winapi_CloseHandle(PyModuleDef *module, PyObject *args) { - HANDLE hObject; + PyObject *return_value = NULL; + HANDLE handle; + + if (!PyArg_ParseTuple(args, + "" F_HANDLE ":CloseHandle", + &handle)) + goto exit; + return_value = _winapi_CloseHandle_impl(module, handle); + +exit: + return return_value; +} + +static PyObject * +_winapi_CloseHandle_impl(PyModuleDef *module, HANDLE handle) +/*[clinic end generated code: checksum=bcb19f463bc38bfb1f2d8822239c4d09e2a2f860]*/ +{ BOOL success; - if (!PyArg_ParseTuple(args, F_HANDLE ":CloseHandle", &hObject)) - return NULL; - Py_BEGIN_ALLOW_THREADS - success = CloseHandle(hObject); + success = CloseHandle(handle); Py_END_ALLOW_THREADS if (!success) @@ -323,44 +439,71 @@ Py_RETURN_NONE; } +/*[clinic input] +_winapi.ConnectNamedPipe + + handle: HANDLE + overlapped: int = 0 +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_ConnectNamedPipe__doc__, +"ConnectNamedPipe(handle, overlapped=0)"); + +#define _WINAPI_CONNECTNAMEDPIPE_METHODDEF \ + {"ConnectNamedPipe", (PyCFunction)_winapi_ConnectNamedPipe, METH_VARARGS|METH_KEYWORDS, _winapi_ConnectNamedPipe__doc__}, + static PyObject * -winapi_ConnectNamedPipe(PyObject *self, PyObject *args, PyObject *kwds) +_winapi_ConnectNamedPipe_impl(PyModuleDef *module, HANDLE handle, int overlapped); + +static PyObject * +_winapi_ConnectNamedPipe(PyModuleDef *module, PyObject *args, PyObject *kwargs) { - HANDLE hNamedPipe; - int use_overlapped = 0; + PyObject *return_value = NULL; + static char *_keywords[] = {"handle", "overlapped", NULL}; + HANDLE handle; + int overlapped = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "" F_HANDLE "|i:ConnectNamedPipe", _keywords, + &handle, &overlapped)) + goto exit; + return_value = _winapi_ConnectNamedPipe_impl(module, handle, overlapped); + +exit: + return return_value; +} + +static PyObject * +_winapi_ConnectNamedPipe_impl(PyModuleDef *module, HANDLE handle, int overlapped) +/*[clinic end generated code: checksum=580d670980289fb75a90e26802abf803aa83e223]*/ +{ BOOL success; - OverlappedObject *overlapped = NULL; - static char *kwlist[] = {"handle", "overlapped", NULL}; + OverlappedObject *olapOb = NULL; - if (!PyArg_ParseTupleAndKeywords(args, kwds, - F_HANDLE "|" F_BOOL, kwlist, - &hNamedPipe, &use_overlapped)) - return NULL; - - if (use_overlapped) { - overlapped = new_overlapped(hNamedPipe); - if (!overlapped) + if (overlapped) { + olapOb = new_overlapped(handle); + if (!olapOb) return NULL; } Py_BEGIN_ALLOW_THREADS - success = ConnectNamedPipe(hNamedPipe, - overlapped ? &overlapped->overlapped : NULL); + success = ConnectNamedPipe(handle, + olapOb ? &olapOb->overlapped : NULL); Py_END_ALLOW_THREADS - if (overlapped) { + if (olapOb) { int err = GetLastError(); /* Overlapped ConnectNamedPipe never returns a success code */ assert(success == 0); if (err == ERROR_IO_PENDING) - overlapped->pending = 1; + olapOb->pending = 1; else if (err == ERROR_PIPE_CONNECTED) - SetEvent(overlapped->overlapped.hEvent); + SetEvent(olapOb->overlapped.hEvent); else { - Py_DECREF(overlapped); + Py_DECREF(olapOb); return PyErr_SetFromWindowsErr(err); } - return (PyObject *) overlapped; + return (PyObject *) olapOb; } if (!success) return PyErr_SetFromWindowsErr(0); @@ -368,9 +511,32 @@ Py_RETURN_NONE; } +/*[clinic input] +_winapi.CreateFile + + lpFileName: LPCTSTR + dwDesiredAccess: DWORD + dwShareMode: DWORD + lpSecurityAttributes: LPSECURITY_ATTRIBUTES + dwCreationDisposition: DWORD + dwFlagsAndAttributes: DWORD + hTemplateFile: HANDLE + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_CreateFile__doc__, +"CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile)"); + +#define _WINAPI_CREATEFILE_METHODDEF \ + {"CreateFile", (PyCFunction)_winapi_CreateFile, METH_VARARGS, _winapi_CreateFile__doc__}, + static PyObject * -winapi_CreateFile(PyObject *self, PyObject *args) +_winapi_CreateFile_impl(PyModuleDef *module, LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile); + +static PyObject * +_winapi_CreateFile(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; LPCTSTR lpFileName; DWORD dwDesiredAccess; DWORD dwShareMode; @@ -378,15 +544,23 @@ DWORD dwCreationDisposition; DWORD dwFlagsAndAttributes; HANDLE hTemplateFile; + + if (!PyArg_ParseTuple(args, + "skk" F_POINTER "kk" F_HANDLE ":CreateFile", + &lpFileName, &dwDesiredAccess, &dwShareMode, &lpSecurityAttributes, &dwCreationDisposition, &dwFlagsAndAttributes, &hTemplateFile)) + goto exit; + return_value = _winapi_CreateFile_impl(module, lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); + +exit: + return return_value; +} + +static PyObject * +_winapi_CreateFile_impl(PyModuleDef *module, LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) +/*[clinic end generated code: checksum=63e982435c8b5e18f36dba1ac85bf0593b041415]*/ +{ HANDLE handle; - if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_POINTER - F_DWORD F_DWORD F_HANDLE, - &lpFileName, &dwDesiredAccess, &dwShareMode, - &lpSecurityAttributes, &dwCreationDisposition, - &dwFlagsAndAttributes, &hTemplateFile)) - return NULL; - Py_BEGIN_ALLOW_THREADS handle = CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, @@ -400,9 +574,33 @@ return Py_BuildValue(F_HANDLE, handle); } +/*[clinic input] +_winapi.CreateNamedPipe + + lpName: LPCTSTR + dwOpenMode: DWORD + dwPipeMode: DWORD + nMaxInstances: DWORD + nOutBufferSize: DWORD + nInBufferSize: DWORD + nDefaultTimeOut: DWORD + lpSecurityAttributes: LPSECURITY_ATTRIBUTES + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_CreateNamedPipe__doc__, +"CreateNamedPipe(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes)"); + +#define _WINAPI_CREATENAMEDPIPE_METHODDEF \ + {"CreateNamedPipe", (PyCFunction)_winapi_CreateNamedPipe, METH_VARARGS, _winapi_CreateNamedPipe__doc__}, + static PyObject * -winapi_CreateNamedPipe(PyObject *self, PyObject *args) +_winapi_CreateNamedPipe_impl(PyModuleDef *module, LPCTSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances, DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes); + +static PyObject * +_winapi_CreateNamedPipe(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; LPCTSTR lpName; DWORD dwOpenMode; DWORD dwPipeMode; @@ -411,16 +609,23 @@ DWORD nInBufferSize; DWORD nDefaultTimeOut; LPSECURITY_ATTRIBUTES lpSecurityAttributes; + + if (!PyArg_ParseTuple(args, + "skkkkkk" F_POINTER ":CreateNamedPipe", + &lpName, &dwOpenMode, &dwPipeMode, &nMaxInstances, &nOutBufferSize, &nInBufferSize, &nDefaultTimeOut, &lpSecurityAttributes)) + goto exit; + return_value = _winapi_CreateNamedPipe_impl(module, lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, nInBufferSize, nDefaultTimeOut, lpSecurityAttributes); + +exit: + return return_value; +} + +static PyObject * +_winapi_CreateNamedPipe_impl(PyModuleDef *module, LPCTSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances, DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes) +/*[clinic end generated code: checksum=94b046c6b65a75932fc19d1fb6a907058e95f11d]*/ +{ HANDLE handle; - if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_DWORD - F_DWORD F_DWORD F_DWORD F_POINTER, - &lpName, &dwOpenMode, &dwPipeMode, - &nMaxInstances, &nOutBufferSize, - &nInBufferSize, &nDefaultTimeOut, - &lpSecurityAttributes)) - return NULL; - Py_BEGIN_ALLOW_THREADS handle = CreateNamedPipe(lpName, dwOpenMode, dwPipeMode, nMaxInstances, nOutBufferSize, @@ -434,28 +639,59 @@ return Py_BuildValue(F_HANDLE, handle); } -PyDoc_STRVAR(CreatePipe_doc, -"CreatePipe(pipe_attrs, size) -> (read_handle, write_handle)\n\ -\n\ -Create an anonymous pipe, and return handles to the read and\n\ -write ends of the pipe.\n\ -\n\ -pipe_attrs is ignored internally and can be None."); +/*[clinic input] +_winapi.CreatePipe + + pipe_attrs: object + Ignored internally, can be None. + size: DWORD + / + +Create an anonymous pipe. + +Returns handles to the read and write ends of the pipe. +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_CreatePipe__doc__, +"CreatePipe(pipe_attrs, size)\n" +"Create an anonymous pipe.\n" +"\n" +" pipe_attrs\n" +" Ignored internally, can be None.\n" +"\n" +"Returns handles to the read and write ends of the pipe."); + +#define _WINAPI_CREATEPIPE_METHODDEF \ + {"CreatePipe", (PyCFunction)_winapi_CreatePipe, METH_VARARGS, _winapi_CreatePipe__doc__}, static PyObject * -winapi_CreatePipe(PyObject* self, PyObject* args) +_winapi_CreatePipe_impl(PyModuleDef *module, PyObject *pipe_attrs, DWORD size); + +static PyObject * +_winapi_CreatePipe(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + PyObject *pipe_attrs; + DWORD size; + + if (!PyArg_ParseTuple(args, + "Ok:CreatePipe", + &pipe_attrs, &size)) + goto exit; + return_value = _winapi_CreatePipe_impl(module, pipe_attrs, size); + +exit: + return return_value; +} + +static PyObject * +_winapi_CreatePipe_impl(PyModuleDef *module, PyObject *pipe_attrs, DWORD size) +/*[clinic end generated code: checksum=a504ec05a074424c184063b94b9c0af16cfddd4b]*/ { HANDLE read_pipe; HANDLE write_pipe; BOOL result; - PyObject* pipe_attributes; /* ignored */ - DWORD size; - - if (! PyArg_ParseTuple(args, "O" F_DWORD ":CreatePipe", - &pipe_attributes, &size)) - return NULL; - Py_BEGIN_ALLOW_THREADS result = CreatePipe(&read_pipe, &write_pipe, NULL, size); Py_END_ALLOW_THREADS @@ -576,20 +812,73 @@ return NULL; } -PyDoc_STRVAR(CreateProcess_doc, -"CreateProcess(app_name, cmd_line, proc_attrs, thread_attrs,\n\ - inherit, flags, env_mapping, curdir,\n\ - startup_info) -> (proc_handle, thread_handle,\n\ - pid, tid)\n\ -\n\ -Create a new process and its primary thread. The return\n\ -value is a tuple of the process handle, thread handle,\n\ -process ID, and thread ID.\n\ -\n\ -proc_attrs and thread_attrs are ignored internally and can be None."); +/*[clinic input] +_winapi.CreateProcess + + application_name: Py_UNICODE(nullable=True) + command_line: Py_UNICODE(nullable=True) + proc_attrs: object + Ignored internally, can be None. + thread_attrs: object + Ignored internally, can be None. + inherit_handles: BOOL + creation_flags: DWORD + env_mapping: object + current_directory: Py_UNICODE(nullable=True) + startup_info: object + / + +Create a new process and its primary thread. + +The return value is a tuple of the process handle, thread handle, +process ID, and thread ID. +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_CreateProcess__doc__, +"CreateProcess(application_name, command_line, proc_attrs, thread_attrs, inherit_handles, creation_flags, env_mapping, current_directory, startup_info)\n" +"Create a new process and its primary thread.\n" +"\n" +" proc_attrs\n" +" Ignored internally, can be None.\n" +" thread_attrs\n" +" Ignored internally, can be None.\n" +"\n" +"The return value is a tuple of the process handle, thread handle,\n" +"process ID, and thread ID."); + +#define _WINAPI_CREATEPROCESS_METHODDEF \ + {"CreateProcess", (PyCFunction)_winapi_CreateProcess, METH_VARARGS, _winapi_CreateProcess__doc__}, static PyObject * -winapi_CreateProcess(PyObject* self, PyObject* args) +_winapi_CreateProcess_impl(PyModuleDef *module, Py_UNICODE *application_name, Py_UNICODE *command_line, PyObject *proc_attrs, PyObject *thread_attrs, BOOL inherit_handles, DWORD creation_flags, PyObject *env_mapping, Py_UNICODE *current_directory, PyObject *startup_info); + +static PyObject * +_winapi_CreateProcess(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_UNICODE *application_name; + Py_UNICODE *command_line; + PyObject *proc_attrs; + PyObject *thread_attrs; + BOOL inherit_handles; + DWORD creation_flags; + PyObject *env_mapping; + Py_UNICODE *current_directory; + PyObject *startup_info; + + if (!PyArg_ParseTuple(args, + "ZZOOikOZO:CreateProcess", + &application_name, &command_line, &proc_attrs, &thread_attrs, &inherit_handles, &creation_flags, &env_mapping, ¤t_directory, &startup_info)) + goto exit; + return_value = _winapi_CreateProcess_impl(module, application_name, command_line, proc_attrs, thread_attrs, inherit_handles, creation_flags, env_mapping, current_directory, startup_info); + +exit: + return return_value; +} + +static PyObject * +_winapi_CreateProcess_impl(PyModuleDef *module, Py_UNICODE *application_name, Py_UNICODE *command_line, PyObject *proc_attrs, PyObject *thread_attrs, BOOL inherit_handles, DWORD creation_flags, PyObject *env_mapping, Py_UNICODE *current_directory, PyObject *startup_info) +/*[clinic end generated code: checksum=1648bb90e2c9fe03de5e2cbf08139365ebe8fd41]*/ { BOOL result; PROCESS_INFORMATION pi; @@ -597,28 +886,6 @@ PyObject* environment; wchar_t *wenvironment; - wchar_t* application_name; - wchar_t* command_line; - PyObject* process_attributes; /* ignored */ - PyObject* thread_attributes; /* ignored */ - BOOL inherit_handles; - DWORD creation_flags; - PyObject* env_mapping; - wchar_t* current_directory; - PyObject* startup_info; - - if (! PyArg_ParseTuple(args, "ZZOO" F_BOOL F_DWORD "OZO:CreateProcess", - &application_name, - &command_line, - &process_attributes, - &thread_attributes, - &inherit_handles, - &creation_flags, - &env_mapping, - ¤t_directory, - &startup_info)) - return NULL; - ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); @@ -672,23 +939,42 @@ pi.dwThreadId); } -PyDoc_STRVAR(DuplicateHandle_doc, -"DuplicateHandle(source_proc_handle, source_handle,\n\ - target_proc_handle, target_handle, access,\n\ - inherit[, options]) -> handle\n\ -\n\ -Return a duplicate handle object.\n\ -\n\ -The duplicate handle refers to the same object as the original\n\ -handle. Therefore, any changes to the object are reflected\n\ -through both handles."); +/*[clinic input] +_winapi.DuplicateHandle + + source_process_handle: HANDLE + source_handle: HANDLE + target_process_handle: HANDLE + desired_access: DWORD + inherit_handle: BOOL + options: DWORD = 0 + / + +Return a duplicate handle object. + +The duplicate handle refers to the same object as the original +handle. Therefore, any changes to the object are reflected +through both handles. +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_DuplicateHandle__doc__, +"DuplicateHandle(source_process_handle, source_handle, target_process_handle, desired_access, inherit_handle, options=0)\n" +"Return a duplicate handle object.\n" +"\n" +"The duplicate handle refers to the same object as the original\n" +"handle. Therefore, any changes to the object are reflected\n" +"through both handles."); + +#define _WINAPI_DUPLICATEHANDLE_METHODDEF \ + {"DuplicateHandle", (PyCFunction)_winapi_DuplicateHandle, METH_VARARGS, _winapi_DuplicateHandle__doc__}, static PyObject * -winapi_DuplicateHandle(PyObject* self, PyObject* args) +_winapi_DuplicateHandle_impl(PyModuleDef *module, HANDLE source_process_handle, HANDLE source_handle, HANDLE target_process_handle, DWORD desired_access, BOOL inherit_handle, DWORD options); + +static PyObject * +_winapi_DuplicateHandle(PyModuleDef *module, PyObject *args) { - HANDLE target_handle; - BOOL result; - + PyObject *return_value = NULL; HANDLE source_process_handle; HANDLE source_handle; HANDLE target_process_handle; @@ -696,16 +982,22 @@ BOOL inherit_handle; DWORD options = 0; - if (! PyArg_ParseTuple(args, - F_HANDLE F_HANDLE F_HANDLE F_DWORD F_BOOL F_DWORD - ":DuplicateHandle", - &source_process_handle, - &source_handle, - &target_process_handle, - &desired_access, - &inherit_handle, - &options)) - return NULL; + if (!PyArg_ParseTuple(args, + "" F_HANDLE "" F_HANDLE "" F_HANDLE "ki|k:DuplicateHandle", + &source_process_handle, &source_handle, &target_process_handle, &desired_access, &inherit_handle, &options)) + goto exit; + return_value = _winapi_DuplicateHandle_impl(module, source_process_handle, source_handle, target_process_handle, desired_access, inherit_handle, options); + +exit: + return return_value; +} + +static PyObject * +_winapi_DuplicateHandle_impl(PyModuleDef *module, HANDLE source_process_handle, HANDLE source_handle, HANDLE target_process_handle, DWORD desired_access, BOOL inherit_handle, DWORD options) +/*[clinic end generated code: checksum=8e6091f29cf30657a81b4b57f4939e539215df10]*/ +{ + HANDLE target_handle; + BOOL result; Py_BEGIN_ALLOW_THREADS result = DuplicateHandle( @@ -725,54 +1017,129 @@ return HANDLE_TO_PYNUM(target_handle); } +/*[clinic input] +_winapi.ExitProcess + + ExitCode: UINT + / + +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_ExitProcess__doc__, +"ExitProcess(ExitCode)"); + +#define _WINAPI_EXITPROCESS_METHODDEF \ + {"ExitProcess", (PyCFunction)_winapi_ExitProcess, METH_VARARGS, _winapi_ExitProcess__doc__}, + static PyObject * -winapi_ExitProcess(PyObject *self, PyObject *args) +_winapi_ExitProcess_impl(PyModuleDef *module, UINT ExitCode); + +static PyObject * +_winapi_ExitProcess(PyModuleDef *module, PyObject *args) { - UINT uExitCode; + PyObject *return_value = NULL; + UINT ExitCode; - if (!PyArg_ParseTuple(args, F_UINT, &uExitCode)) - return NULL; + if (!PyArg_ParseTuple(args, + "I:ExitProcess", + &ExitCode)) + goto exit; + return_value = _winapi_ExitProcess_impl(module, ExitCode); +exit: + return return_value; +} + +static PyObject * +_winapi_ExitProcess_impl(PyModuleDef *module, UINT ExitCode) +/*[clinic end generated code: checksum=0706182c4e3168bedef8dc03fad247b8d7f2258c]*/ +{ #if defined(Py_DEBUG) SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT| SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX); _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); #endif - ExitProcess(uExitCode); + ExitProcess(ExitCode); return NULL; } -PyDoc_STRVAR(GetCurrentProcess_doc, -"GetCurrentProcess() -> handle\n\ -\n\ -Return a handle object for the current process."); +/*[clinic input] +_winapi.GetCurrentProcess + +Return a handle object for the current process. +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_GetCurrentProcess__doc__, +"GetCurrentProcess()\n" +"Return a handle object for the current process."); + +#define _WINAPI_GETCURRENTPROCESS_METHODDEF \ + {"GetCurrentProcess", (PyCFunction)_winapi_GetCurrentProcess, METH_NOARGS, _winapi_GetCurrentProcess__doc__}, static PyObject * -winapi_GetCurrentProcess(PyObject* self, PyObject* args) +_winapi_GetCurrentProcess_impl(PyModuleDef *module); + +static PyObject * +_winapi_GetCurrentProcess(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) { - if (! PyArg_ParseTuple(args, ":GetCurrentProcess")) - return NULL; + PyObject *return_value = NULL; + return_value = _winapi_GetCurrentProcess_impl(module); + + return return_value; +} + +static PyObject * +_winapi_GetCurrentProcess_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=80a21442d1202e0b67a0e69c1889460cd8b50fe1]*/ +{ return HANDLE_TO_PYNUM(GetCurrentProcess()); } -PyDoc_STRVAR(GetExitCodeProcess_doc, -"GetExitCodeProcess(handle) -> Exit code\n\ -\n\ -Return the termination status of the specified process."); +/*[clinic input] +_winapi.GetExitCodeProcess + + process: HANDLE + / + +Return the termination status of the specified process. +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_GetExitCodeProcess__doc__, +"GetExitCodeProcess(process)\n" +"Return the termination status of the specified process."); + +#define _WINAPI_GETEXITCODEPROCESS_METHODDEF \ + {"GetExitCodeProcess", (PyCFunction)_winapi_GetExitCodeProcess, METH_VARARGS, _winapi_GetExitCodeProcess__doc__}, static PyObject * -winapi_GetExitCodeProcess(PyObject* self, PyObject* args) +_winapi_GetExitCodeProcess_impl(PyModuleDef *module, HANDLE process); + +static PyObject * +_winapi_GetExitCodeProcess(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + HANDLE process; + + if (!PyArg_ParseTuple(args, + "" F_HANDLE ":GetExitCodeProcess", + &process)) + goto exit; + return_value = _winapi_GetExitCodeProcess_impl(module, process); + +exit: + return return_value; +} + +static PyObject * +_winapi_GetExitCodeProcess_impl(PyModuleDef *module, HANDLE process) +/*[clinic end generated code: checksum=5a9b0efd44aaa8e3be9ffe1ef5c83d415e96dc9a]*/ { DWORD exit_code; BOOL result; - HANDLE process; - if (! PyArg_ParseTuple(args, F_HANDLE ":GetExitCodeProcess", &process)) - return NULL; - result = GetExitCodeProcess(process, &exit_code); if (! result) @@ -781,36 +1148,93 @@ return PyLong_FromUnsignedLong(exit_code); } +/*[clinic input] +_winapi.GetLastError +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_GetLastError__doc__, +"GetLastError()"); + +#define _WINAPI_GETLASTERROR_METHODDEF \ + {"GetLastError", (PyCFunction)_winapi_GetLastError, METH_NOARGS, _winapi_GetLastError__doc__}, + static PyObject * -winapi_GetLastError(PyObject *self, PyObject *args) +_winapi_GetLastError_impl(PyModuleDef *module); + +static PyObject * +_winapi_GetLastError(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = _winapi_GetLastError_impl(module); + + return return_value; +} + +static PyObject * +_winapi_GetLastError_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=181a3ae189ee82a1b2a0a84ea8e2d16ef9e35af2]*/ { return Py_BuildValue(F_DWORD, GetLastError()); } -PyDoc_STRVAR(GetModuleFileName_doc, -"GetModuleFileName(module) -> path\n\ -\n\ -Return the fully-qualified path for the file that contains\n\ -the specified module. The module must have been loaded by the\n\ -current process.\n\ -\n\ -The module parameter should be a handle to the loaded module\n\ -whose path is being requested. If this parameter is 0, \n\ -GetModuleFileName retrieves the path of the executable file\n\ -of the current process."); +/*[clinic input] +_winapi.GetModuleFileName + + module: HMODULE + / + +Return the fully-qualified path for the file that contains module. + +The module must have been loaded by the current process. + +The module parameter should be a handle to the loaded module +whose path is being requested. If this parameter is 0, +GetModuleFileName retrieves the path of the executable file +of the current process. +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_GetModuleFileName__doc__, +"GetModuleFileName(module)\n" +"Return the fully-qualified path for the file that contains module.\n" +"\n" +"The module must have been loaded by the current process.\n" +"\n" +"The module parameter should be a handle to the loaded module\n" +"whose path is being requested. If this parameter is 0,\n" +"GetModuleFileName retrieves the path of the executable file\n" +"of the current process."); + +#define _WINAPI_GETMODULEFILENAME_METHODDEF \ + {"GetModuleFileName", (PyCFunction)_winapi_GetModuleFileName, METH_VARARGS, _winapi_GetModuleFileName__doc__}, static PyObject * -winapi_GetModuleFileName(PyObject* self, PyObject* args) +_winapi_GetModuleFileName_impl(PyModuleDef *module, HMODULE module_value); + +static PyObject * +_winapi_GetModuleFileName(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + HMODULE module_value; + + if (!PyArg_ParseTuple(args, + "" F_HANDLE ":GetModuleFileName", + &module_value)) + goto exit; + return_value = _winapi_GetModuleFileName_impl(module, module_value); + +exit: + return return_value; +} + +static PyObject * +_winapi_GetModuleFileName_impl(PyModuleDef *module, HMODULE module_value) +/*[clinic end generated code: checksum=8273a0c4437dbeac637e1e22efd9187baf95cda0]*/ { BOOL result; - HMODULE module; WCHAR filename[MAX_PATH]; - if (! PyArg_ParseTuple(args, F_HANDLE ":GetModuleFileName", - &module)) - return NULL; - - result = GetModuleFileNameW(module, filename, MAX_PATH); + result = GetModuleFileNameW(module_value, filename, MAX_PATH); filename[MAX_PATH-1] = '\0'; if (! result) @@ -819,21 +1243,54 @@ return PyUnicode_FromWideChar(filename, wcslen(filename)); } -PyDoc_STRVAR(GetStdHandle_doc, -"GetStdHandle(handle) -> integer\n\ -\n\ -Return a handle to the specified standard device\n\ -(STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE).\n\ -The integer associated with the handle object is returned."); +/*[clinic input] +_winapi.GetStdHandle + + std_handle: DWORD + One of STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, or STD_ERROR_HANDLE. + / + +Return a handle to the specified standard device. + +The integer associated with the handle object is returned. +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_GetStdHandle__doc__, +"GetStdHandle(std_handle)\n" +"Return a handle to the specified standard device.\n" +"\n" +" std_handle\n" +" One of STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, or STD_ERROR_HANDLE.\n" +"\n" +"The integer associated with the handle object is returned."); + +#define _WINAPI_GETSTDHANDLE_METHODDEF \ + {"GetStdHandle", (PyCFunction)_winapi_GetStdHandle, METH_VARARGS, _winapi_GetStdHandle__doc__}, static PyObject * -winapi_GetStdHandle(PyObject* self, PyObject* args) +_winapi_GetStdHandle_impl(PyModuleDef *module, DWORD std_handle); + +static PyObject * +_winapi_GetStdHandle(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + DWORD std_handle; + + if (!PyArg_ParseTuple(args, + "k:GetStdHandle", + &std_handle)) + goto exit; + return_value = _winapi_GetStdHandle_impl(module, std_handle); + +exit: + return return_value; +} + +static PyObject * +_winapi_GetStdHandle_impl(PyModuleDef *module, DWORD std_handle) +/*[clinic end generated code: checksum=dfdc9f6c5e7fa7ea7fea028a611928618e4b1fc6]*/ { HANDLE handle; - DWORD std_handle; - - if (! PyArg_ParseTuple(args, F_DWORD ":GetStdHandle", &std_handle)) - return NULL; Py_BEGIN_ALLOW_THREADS handle = GetStdHandle(std_handle); @@ -851,32 +1308,81 @@ return HANDLE_TO_PYNUM(handle); } -PyDoc_STRVAR(GetVersion_doc, -"GetVersion() -> version\n\ -\n\ -Return the version number of the current operating system."); +/*[clinic input] +_winapi.GetVersion + +Return the version number of the current operating system. +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_GetVersion__doc__, +"GetVersion()\n" +"Return the version number of the current operating system."); + +#define _WINAPI_GETVERSION_METHODDEF \ + {"GetVersion", (PyCFunction)_winapi_GetVersion, METH_NOARGS, _winapi_GetVersion__doc__}, static PyObject * -winapi_GetVersion(PyObject* self, PyObject* args) +_winapi_GetVersion_impl(PyModuleDef *module); + +static PyObject * +_winapi_GetVersion(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) { - if (! PyArg_ParseTuple(args, ":GetVersion")) - return NULL; + PyObject *return_value = NULL; + return_value = _winapi_GetVersion_impl(module); + + return return_value; +} + +static PyObject * +_winapi_GetVersion_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=ec01901792cbcc56cfa1bd2dfe045a11c74cda89]*/ +{ return PyLong_FromUnsignedLong(GetVersion()); } +/*[clinic input] +_winapi.OpenProcess + + dwDesiredAccess: DWORD + bInheritHandle: BOOL + dwProcessId: DWORD + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_OpenProcess__doc__, +"OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId)"); + +#define _WINAPI_OPENPROCESS_METHODDEF \ + {"OpenProcess", (PyCFunction)_winapi_OpenProcess, METH_VARARGS, _winapi_OpenProcess__doc__}, + static PyObject * -winapi_OpenProcess(PyObject *self, PyObject *args) +_winapi_OpenProcess_impl(PyModuleDef *module, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId); + +static PyObject * +_winapi_OpenProcess(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; DWORD dwDesiredAccess; BOOL bInheritHandle; DWORD dwProcessId; + + if (!PyArg_ParseTuple(args, + "kik:OpenProcess", + &dwDesiredAccess, &bInheritHandle, &dwProcessId)) + goto exit; + return_value = _winapi_OpenProcess_impl(module, dwDesiredAccess, bInheritHandle, dwProcessId); + +exit: + return return_value; +} + +static PyObject * +_winapi_OpenProcess_impl(PyModuleDef *module, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) +/*[clinic end generated code: checksum=6fae4283f1c9febdb7203196522117cea767a808]*/ +{ HANDLE handle; - if (!PyArg_ParseTuple(args, F_DWORD F_BOOL F_DWORD, - &dwDesiredAccess, &bInheritHandle, &dwProcessId)) - return NULL; - handle = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); if (handle == NULL) return PyErr_SetFromWindowsErr(0); @@ -884,18 +1390,48 @@ return Py_BuildValue(F_HANDLE, handle); } +/*[clinic input] +_winapi.PeekNamedPipe + + handle: HANDLE + size: int = 0 + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_PeekNamedPipe__doc__, +"PeekNamedPipe(handle, size=0)"); + +#define _WINAPI_PEEKNAMEDPIPE_METHODDEF \ + {"PeekNamedPipe", (PyCFunction)_winapi_PeekNamedPipe, METH_VARARGS, _winapi_PeekNamedPipe__doc__}, + static PyObject * -winapi_PeekNamedPipe(PyObject *self, PyObject *args) +_winapi_PeekNamedPipe_impl(PyModuleDef *module, HANDLE handle, int size); + +static PyObject * +_winapi_PeekNamedPipe(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; HANDLE handle; int size = 0; + + if (!PyArg_ParseTuple(args, + "" F_HANDLE "|i:PeekNamedPipe", + &handle, &size)) + goto exit; + return_value = _winapi_PeekNamedPipe_impl(module, handle, size); + +exit: + return return_value; +} + +static PyObject * +_winapi_PeekNamedPipe_impl(PyModuleDef *module, HANDLE handle, int size) +/*[clinic end generated code: checksum=070174652e267e3eb1dc1dc4817e2570a6c08969]*/ +{ PyObject *buf = NULL; DWORD nread, navail, nleft; BOOL ret; - if (!PyArg_ParseTuple(args, F_HANDLE "|i:PeekNamedPipe" , &handle, &size)) - return NULL; - if (size < 0) { PyErr_SetString(PyExc_ValueError, "negative size"); return NULL; @@ -928,54 +1464,82 @@ } } +/*[clinic input] +_winapi.ReadFile + + handle: HANDLE + size: int + overlapped: int = 0 +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_ReadFile__doc__, +"ReadFile(handle, size, overlapped=0)"); + +#define _WINAPI_READFILE_METHODDEF \ + {"ReadFile", (PyCFunction)_winapi_ReadFile, METH_VARARGS|METH_KEYWORDS, _winapi_ReadFile__doc__}, + static PyObject * -winapi_ReadFile(PyObject *self, PyObject *args, PyObject *kwds) +_winapi_ReadFile_impl(PyModuleDef *module, HANDLE handle, int size, int overlapped); + +static PyObject * +_winapi_ReadFile(PyModuleDef *module, PyObject *args, PyObject *kwargs) { + PyObject *return_value = NULL; + static char *_keywords[] = {"handle", "size", "overlapped", NULL}; HANDLE handle; int size; + int overlapped = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "" F_HANDLE "i|i:ReadFile", _keywords, + &handle, &size, &overlapped)) + goto exit; + return_value = _winapi_ReadFile_impl(module, handle, size, overlapped); + +exit: + return return_value; +} + +static PyObject * +_winapi_ReadFile_impl(PyModuleDef *module, HANDLE handle, int size, int overlapped) +/*[clinic end generated code: checksum=5a89623e1cb495c10d614e9dc8b7ed033ea8cfe8]*/ +{ DWORD nread; PyObject *buf; BOOL ret; - int use_overlapped = 0; DWORD err; - OverlappedObject *overlapped = NULL; - static char *kwlist[] = {"handle", "size", "overlapped", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, - F_HANDLE "i|i:ReadFile", kwlist, - &handle, &size, &use_overlapped)) - return NULL; + OverlappedObject *olapOb = NULL; buf = PyBytes_FromStringAndSize(NULL, size); if (!buf) return NULL; - if (use_overlapped) { - overlapped = new_overlapped(handle); - if (!overlapped) { + if (overlapped) { + olapOb = new_overlapped(handle); + if (!olapOb) { Py_DECREF(buf); return NULL; } /* Steals reference to buf */ - overlapped->read_buffer = buf; + olapOb->read_buffer = buf; } Py_BEGIN_ALLOW_THREADS ret = ReadFile(handle, PyBytes_AS_STRING(buf), size, &nread, - overlapped ? &overlapped->overlapped : NULL); + olapOb ? &olapOb->overlapped : NULL); Py_END_ALLOW_THREADS err = ret ? 0 : GetLastError(); - if (overlapped) { + if (olapOb) { if (!ret) { if (err == ERROR_IO_PENDING) - overlapped->pending = 1; + olapOb->pending = 1; else if (err != ERROR_MORE_DATA) { - Py_DECREF(overlapped); + Py_DECREF(olapOb); return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); } } - return Py_BuildValue("NI", (PyObject *) overlapped, err); + return Py_BuildValue("NI", (PyObject *) olapOb, err); } if (!ret && err != ERROR_MORE_DATA) { @@ -987,18 +1551,52 @@ return Py_BuildValue("NI", buf, err); } +/*[clinic input] +_winapi.SetNamedPipeHandleState + + NamedPipe: HANDLE + arg1: object + arg2: object + arg3: object + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_SetNamedPipeHandleState__doc__, +"SetNamedPipeHandleState(NamedPipe, arg1, arg2, arg3)"); + +#define _WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF \ + {"SetNamedPipeHandleState", (PyCFunction)_winapi_SetNamedPipeHandleState, METH_VARARGS, _winapi_SetNamedPipeHandleState__doc__}, + static PyObject * -winapi_SetNamedPipeHandleState(PyObject *self, PyObject *args) +_winapi_SetNamedPipeHandleState_impl(PyModuleDef *module, HANDLE NamedPipe, PyObject *arg1, PyObject *arg2, PyObject *arg3); + +static PyObject * +_winapi_SetNamedPipeHandleState(PyModuleDef *module, PyObject *args) { - HANDLE hNamedPipe; - PyObject *oArgs[3]; + PyObject *return_value = NULL; + HANDLE NamedPipe; + PyObject *arg1; + PyObject *arg2; + PyObject *arg3; + + if (!PyArg_ParseTuple(args, + "" F_HANDLE "OOO:SetNamedPipeHandleState", + &NamedPipe, &arg1, &arg2, &arg3)) + goto exit; + return_value = _winapi_SetNamedPipeHandleState_impl(module, NamedPipe, arg1, arg2, arg3); + +exit: + return return_value; +} + +static PyObject * +_winapi_SetNamedPipeHandleState_impl(PyModuleDef *module, HANDLE NamedPipe, PyObject *arg1, PyObject *arg2, PyObject *arg3) +/*[clinic end generated code: checksum=d3bdfeaf4fc5169f788ab6f9307d7bce199770b3]*/ +{ + PyObject *oArgs[3] = {arg1, arg2, arg3}; DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL}; int i; - if (!PyArg_ParseTuple(args, F_HANDLE "OOO", - &hNamedPipe, &oArgs[0], &oArgs[1], &oArgs[2])) - return NULL; - PyErr_Clear(); for (i = 0 ; i < 3 ; i++) { @@ -1010,29 +1608,57 @@ } } - if (!SetNamedPipeHandleState(hNamedPipe, pArgs[0], pArgs[1], pArgs[2])) + if (!SetNamedPipeHandleState(NamedPipe, pArgs[0], pArgs[1], pArgs[2])) return PyErr_SetFromWindowsErr(0); Py_RETURN_NONE; } -PyDoc_STRVAR(TerminateProcess_doc, -"TerminateProcess(handle, exit_code) -> None\n\ -\n\ -Terminate the specified process and all of its threads."); + +/*[clinic input] +_winapi.TerminateProcess + + handle: HANDLE + exit_code: UINT + / + +Terminate the specified process and all of its threads. +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_TerminateProcess__doc__, +"TerminateProcess(handle, exit_code)\n" +"Terminate the specified process and all of its threads."); + +#define _WINAPI_TERMINATEPROCESS_METHODDEF \ + {"TerminateProcess", (PyCFunction)_winapi_TerminateProcess, METH_VARARGS, _winapi_TerminateProcess__doc__}, static PyObject * -winapi_TerminateProcess(PyObject* self, PyObject* args) +_winapi_TerminateProcess_impl(PyModuleDef *module, HANDLE handle, UINT exit_code); + +static PyObject * +_winapi_TerminateProcess(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + HANDLE handle; + UINT exit_code; + + if (!PyArg_ParseTuple(args, + "" F_HANDLE "I:TerminateProcess", + &handle, &exit_code)) + goto exit; + return_value = _winapi_TerminateProcess_impl(module, handle, exit_code); + +exit: + return return_value; +} + +static PyObject * +_winapi_TerminateProcess_impl(PyModuleDef *module, HANDLE handle, UINT exit_code) +/*[clinic end generated code: checksum=b43446616f5a0e97ef3ff85dc37576f20328f47f]*/ { BOOL result; - HANDLE process; - UINT exit_code; - if (! PyArg_ParseTuple(args, F_HANDLE F_UINT ":TerminateProcess", - &process, &exit_code)) - return NULL; - - result = TerminateProcess(process, exit_code); + result = TerminateProcess(handle, exit_code); if (! result) return PyErr_SetFromWindowsErr(GetLastError()); @@ -1041,18 +1667,48 @@ return Py_None; } +/*[clinic input] +_winapi.WaitNamedPipe + + NamedPipeName: LPCTSTR + TimeOut: DWORD + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_WaitNamedPipe__doc__, +"WaitNamedPipe(NamedPipeName, TimeOut)"); + +#define _WINAPI_WAITNAMEDPIPE_METHODDEF \ + {"WaitNamedPipe", (PyCFunction)_winapi_WaitNamedPipe, METH_VARARGS, _winapi_WaitNamedPipe__doc__}, + static PyObject * -winapi_WaitNamedPipe(PyObject *self, PyObject *args) +_winapi_WaitNamedPipe_impl(PyModuleDef *module, LPCTSTR NamedPipeName, DWORD TimeOut); + +static PyObject * +_winapi_WaitNamedPipe(PyModuleDef *module, PyObject *args) { - LPCTSTR lpNamedPipeName; - DWORD nTimeOut; + PyObject *return_value = NULL; + LPCTSTR NamedPipeName; + DWORD TimeOut; + + if (!PyArg_ParseTuple(args, + "sk:WaitNamedPipe", + &NamedPipeName, &TimeOut)) + goto exit; + return_value = _winapi_WaitNamedPipe_impl(module, NamedPipeName, TimeOut); + +exit: + return return_value; +} + +static PyObject * +_winapi_WaitNamedPipe_impl(PyModuleDef *module, LPCTSTR NamedPipeName, DWORD TimeOut) +/*[clinic end generated code: checksum=d2512566e04ebf73ae3b986f1e77ce4d4bfb693c]*/ +{ BOOL success; - if (!PyArg_ParseTuple(args, "s" F_DWORD, &lpNamedPipeName, &nTimeOut)) - return NULL; - Py_BEGIN_ALLOW_THREADS - success = WaitNamedPipe(lpNamedPipeName, nTimeOut); + success = WaitNamedPipe(NamedPipeName, TimeOut); Py_END_ALLOW_THREADS if (!success) @@ -1061,21 +1717,50 @@ Py_RETURN_NONE; } +/*[clinic input] +_winapi.WaitForMultipleObjects + + handle_seq: object + wait_flag: BOOL + milliseconds: DWORD(c_default='INFINITE') = _winapi.INFINITE + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_WaitForMultipleObjects__doc__, +"WaitForMultipleObjects(handle_seq, wait_flag, milliseconds=_winapi.INFINITE)"); + +#define _WINAPI_WAITFORMULTIPLEOBJECTS_METHODDEF \ + {"WaitForMultipleObjects", (PyCFunction)_winapi_WaitForMultipleObjects, METH_VARARGS, _winapi_WaitForMultipleObjects__doc__}, + static PyObject * -winapi_WaitForMultipleObjects(PyObject* self, PyObject* args) +_winapi_WaitForMultipleObjects_impl(PyModuleDef *module, PyObject *handle_seq, BOOL wait_flag, DWORD milliseconds); + +static PyObject * +_winapi_WaitForMultipleObjects(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + PyObject *handle_seq; + BOOL wait_flag; + DWORD milliseconds = INFINITE; + + if (!PyArg_ParseTuple(args, + "Oi|k:WaitForMultipleObjects", + &handle_seq, &wait_flag, &milliseconds)) + goto exit; + return_value = _winapi_WaitForMultipleObjects_impl(module, handle_seq, wait_flag, milliseconds); + +exit: + return return_value; +} + +static PyObject * +_winapi_WaitForMultipleObjects_impl(PyModuleDef *module, PyObject *handle_seq, BOOL wait_flag, DWORD milliseconds) +/*[clinic end generated code: checksum=7be968ef4226810278d99e238daa6fd999d01854]*/ { DWORD result; - PyObject *handle_seq; HANDLE handles[MAXIMUM_WAIT_OBJECTS]; HANDLE sigint_event = NULL; Py_ssize_t nhandles, i; - BOOL wait_flag; - DWORD milliseconds = INFINITE; - - if (!PyArg_ParseTuple(args, "O" F_BOOL "|" F_DWORD - ":WaitForMultipleObjects", - &handle_seq, &wait_flag, &milliseconds)) - return NULL; if (!PySequence_Check(handle_seq)) { PyErr_Format(PyExc_TypeError, @@ -1129,25 +1814,57 @@ return PyLong_FromLong((int) result); } -PyDoc_STRVAR(WaitForSingleObject_doc, -"WaitForSingleObject(handle, timeout) -> result\n\ -\n\ -Wait until the specified object is in the signaled state or\n\ -the time-out interval elapses. The timeout value is specified\n\ -in milliseconds."); +/*[clinic input] +_winapi.WaitForSingleObject + + handle: HANDLE + milliseconds: DWORD + / + +Wait for a single object. + +Wait until the specified object is in the signaled state or +the time-out interval elapses. The timeout value is specified +in milliseconds. +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_WaitForSingleObject__doc__, +"WaitForSingleObject(handle, milliseconds)\n" +"Wait for a single object.\n" +"\n" +"Wait until the specified object is in the signaled state or\n" +"the time-out interval elapses. The timeout value is specified\n" +"in milliseconds."); + +#define _WINAPI_WAITFORSINGLEOBJECT_METHODDEF \ + {"WaitForSingleObject", (PyCFunction)_winapi_WaitForSingleObject, METH_VARARGS, _winapi_WaitForSingleObject__doc__}, static PyObject * -winapi_WaitForSingleObject(PyObject* self, PyObject* args) +_winapi_WaitForSingleObject_impl(PyModuleDef *module, HANDLE handle, DWORD milliseconds); + +static PyObject * +_winapi_WaitForSingleObject(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + HANDLE handle; + DWORD milliseconds; + + if (!PyArg_ParseTuple(args, + "" F_HANDLE "k:WaitForSingleObject", + &handle, &milliseconds)) + goto exit; + return_value = _winapi_WaitForSingleObject_impl(module, handle, milliseconds); + +exit: + return return_value; +} + +static PyObject * +_winapi_WaitForSingleObject_impl(PyModuleDef *module, HANDLE handle, DWORD milliseconds) +/*[clinic end generated code: checksum=b0e6f7bca18e42944bf4f92cc615f15c468a3840]*/ { DWORD result; - HANDLE handle; - DWORD milliseconds; - if (! PyArg_ParseTuple(args, F_HANDLE F_DWORD ":WaitForSingleObject", - &handle, - &milliseconds)) - return NULL; - Py_BEGIN_ALLOW_THREADS result = WaitForSingleObject(handle, milliseconds); Py_END_ALLOW_THREADS @@ -1158,57 +1875,84 @@ return PyLong_FromUnsignedLong(result); } +/*[clinic input] +_winapi.WriteFile + + handle: HANDLE + buffer: object + overlapped: int = 0 +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_WriteFile__doc__, +"WriteFile(handle, buffer, overlapped=0)"); + +#define _WINAPI_WRITEFILE_METHODDEF \ + {"WriteFile", (PyCFunction)_winapi_WriteFile, METH_VARARGS|METH_KEYWORDS, _winapi_WriteFile__doc__}, + static PyObject * -winapi_WriteFile(PyObject *self, PyObject *args, PyObject *kwds) +_winapi_WriteFile_impl(PyModuleDef *module, HANDLE handle, PyObject *buffer, int overlapped); + +static PyObject * +_winapi_WriteFile(PyModuleDef *module, PyObject *args, PyObject *kwargs) { + PyObject *return_value = NULL; + static char *_keywords[] = {"handle", "buffer", "overlapped", NULL}; HANDLE handle; + PyObject *buffer; + int overlapped = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "" F_HANDLE "O|i:WriteFile", _keywords, + &handle, &buffer, &overlapped)) + goto exit; + return_value = _winapi_WriteFile_impl(module, handle, buffer, overlapped); + +exit: + return return_value; +} + +static PyObject * +_winapi_WriteFile_impl(PyModuleDef *module, HANDLE handle, PyObject *buffer, int overlapped) +/*[clinic end generated code: checksum=2e8cb49620176a65441c5fac1b8fb6545b78b970]*/ +{ Py_buffer _buf, *buf; - PyObject *bufobj; DWORD len, written; BOOL ret; - int use_overlapped = 0; DWORD err; - OverlappedObject *overlapped = NULL; - static char *kwlist[] = {"handle", "buffer", "overlapped", NULL}; + OverlappedObject *olapOb = NULL; - /* First get handle and use_overlapped to know which Py_buffer to use */ - if (!PyArg_ParseTupleAndKeywords(args, kwds, - F_HANDLE "O|i:WriteFile", kwlist, - &handle, &bufobj, &use_overlapped)) - return NULL; - - if (use_overlapped) { - overlapped = new_overlapped(handle); - if (!overlapped) + if (overlapped) { + olapOb = new_overlapped(handle); + if (!olapOb) return NULL; - buf = &overlapped->write_buffer; + buf = &olapOb->write_buffer; } else buf = &_buf; - if (!PyArg_Parse(bufobj, "y*", buf)) { - Py_XDECREF(overlapped); + if (!PyArg_Parse(buffer, "y*", buf)) { + Py_XDECREF(olapOb); return NULL; } Py_BEGIN_ALLOW_THREADS len = (DWORD)Py_MIN(buf->len, DWORD_MAX); ret = WriteFile(handle, buf->buf, len, &written, - overlapped ? &overlapped->overlapped : NULL); + olapOb ? &olapOb->overlapped : NULL); Py_END_ALLOW_THREADS err = ret ? 0 : GetLastError(); - if (overlapped) { + if (olapOb) { if (!ret) { if (err == ERROR_IO_PENDING) - overlapped->pending = 1; + olapOb->pending = 1; else { - Py_DECREF(overlapped); + Py_DECREF(olapOb); return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); } } - return Py_BuildValue("NI", (PyObject *) overlapped, err); + return Py_BuildValue("NI", (PyObject *) olapOb, err); } PyBuffer_Release(buf); @@ -1219,52 +1963,29 @@ static PyMethodDef winapi_functions[] = { - {"CloseHandle", winapi_CloseHandle, METH_VARARGS, - CloseHandle_doc}, - {"ConnectNamedPipe", (PyCFunction)winapi_ConnectNamedPipe, - METH_VARARGS | METH_KEYWORDS, ""}, - {"CreateFile", winapi_CreateFile, METH_VARARGS, - ""}, - {"CreateNamedPipe", winapi_CreateNamedPipe, METH_VARARGS, - ""}, - {"CreatePipe", winapi_CreatePipe, METH_VARARGS, - CreatePipe_doc}, - {"CreateProcess", winapi_CreateProcess, METH_VARARGS, - CreateProcess_doc}, - {"DuplicateHandle", winapi_DuplicateHandle, METH_VARARGS, - DuplicateHandle_doc}, - {"ExitProcess", winapi_ExitProcess, METH_VARARGS, - ""}, - {"GetCurrentProcess", winapi_GetCurrentProcess, METH_VARARGS, - GetCurrentProcess_doc}, - {"GetExitCodeProcess", winapi_GetExitCodeProcess, METH_VARARGS, - GetExitCodeProcess_doc}, - {"GetLastError", winapi_GetLastError, METH_NOARGS, - GetCurrentProcess_doc}, - {"GetModuleFileName", winapi_GetModuleFileName, METH_VARARGS, - GetModuleFileName_doc}, - {"GetStdHandle", winapi_GetStdHandle, METH_VARARGS, - GetStdHandle_doc}, - {"GetVersion", winapi_GetVersion, METH_VARARGS, - GetVersion_doc}, - {"OpenProcess", winapi_OpenProcess, METH_VARARGS, - ""}, - {"PeekNamedPipe", winapi_PeekNamedPipe, METH_VARARGS, - ""}, - {"ReadFile", (PyCFunction)winapi_ReadFile, METH_VARARGS | METH_KEYWORDS, - ""}, - {"SetNamedPipeHandleState", winapi_SetNamedPipeHandleState, METH_VARARGS, - ""}, - {"TerminateProcess", winapi_TerminateProcess, METH_VARARGS, - TerminateProcess_doc}, - {"WaitNamedPipe", winapi_WaitNamedPipe, METH_VARARGS, - ""}, - {"WaitForMultipleObjects", winapi_WaitForMultipleObjects, METH_VARARGS, - ""}, - {"WaitForSingleObject", winapi_WaitForSingleObject, METH_VARARGS, - WaitForSingleObject_doc}, - {"WriteFile", (PyCFunction)winapi_WriteFile, METH_VARARGS | METH_KEYWORDS, - ""}, + _WINAPI_CLOSEHANDLE_METHODDEF + _WINAPI_CONNECTNAMEDPIPE_METHODDEF + _WINAPI_CREATEFILE_METHODDEF + _WINAPI_CREATENAMEDPIPE_METHODDEF + _WINAPI_CREATEPIPE_METHODDEF + _WINAPI_CREATEPROCESS_METHODDEF + _WINAPI_DUPLICATEHANDLE_METHODDEF + _WINAPI_EXITPROCESS_METHODDEF + _WINAPI_GETCURRENTPROCESS_METHODDEF + _WINAPI_GETEXITCODEPROCESS_METHODDEF + _WINAPI_GETLASTERROR_METHODDEF + _WINAPI_GETMODULEFILENAME_METHODDEF + _WINAPI_GETSTDHANDLE_METHODDEF + _WINAPI_GETVERSION_METHODDEF + _WINAPI_OPENPROCESS_METHODDEF + _WINAPI_PEEKNAMEDPIPE_METHODDEF + _WINAPI_READFILE_METHODDEF + _WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF + _WINAPI_TERMINATEPROCESS_METHODDEF + _WINAPI_WAITNAMEDPIPE_METHODDEF + _WINAPI_WAITFORMULTIPLEOBJECTS_METHODDEF + _WINAPI_WAITFORSINGLEOBJECT_METHODDEF + _WINAPI_WRITEFILE_METHODDEF {NULL, NULL} };