diff -r cd3fdf21a6e4 -r 8cf882d09d3f Modules/_winapi.c --- a/Modules/_winapi.c Thu Jan 16 14:15:03 2014 -0800 +++ b/Modules/_winapi.c Fri Jan 17 01:38:15 2014 -0600 @@ -57,13 +57,56 @@ #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') + +class OverlappedObject_converter(self_converter): type='OverlappedObject *' + +class HANDLE_return_converter(CReturnConverter): + type = 'HANDLE' + + def render(self, function, data): + self.declare(data) + self.err_occurred_if("_return_value == INVALID_HANDLE_VALUE", data) + data.return_conversion.append( + 'if (_return_value == NULL)\n Py_RETURN_NONE;\n') + data.return_conversion.append( + 'return_value = HANDLE_TO_PYNUM(_return_value);\n') + +class DWORD_return_converter(CReturnConverter): + type = 'DWORD' + + def render(self, function, data): + self.declare(data) + data.return_conversion.append( + 'return_value = Py_BuildValue("k", _return_value);\n') +[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,17 +189,47 @@ PyObject_Del(self); } +/*[clinic input] +_winapi.Overlapped.GetOverlappedResult + + self: OverlappedObject + wait: bool + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_Overlapped_GetOverlappedResult__doc__, +"GetOverlappedResult(wait)"); + +#define _WINAPI_OVERLAPPED_GETOVERLAPPEDRESULT_METHODDEF \ + {"GetOverlappedResult", (PyCFunction)_winapi_Overlapped_GetOverlappedResult, METH_VARARGS, _winapi_Overlapped_GetOverlappedResult__doc__}, + static PyObject * -overlapped_GetOverlappedResult(OverlappedObject *self, PyObject *waitobj) +_winapi_Overlapped_GetOverlappedResult_impl(OverlappedObject *self, int wait); + +static PyObject * +_winapi_Overlapped_GetOverlappedResult(PyObject *self, PyObject *args) { + PyObject *return_value = NULL; int wait; + + if (!PyArg_ParseTuple(args, + "p:GetOverlappedResult", + &wait)) + goto exit; + return_value = _winapi_Overlapped_GetOverlappedResult_impl((OverlappedObject *)self, wait); + +exit: + return return_value; +} + +static PyObject * +_winapi_Overlapped_GetOverlappedResult_impl(OverlappedObject *self, int wait) +/*[clinic end generated code: checksum=61903eb2293b6b3582c8bd061c8abf55120c69f6]*/ +{ BOOL res; DWORD transferred = 0; DWORD err; - wait = PyObject_IsTrue(waitobj); - if (wait < 0) - return NULL; Py_BEGIN_ALLOW_THREADS res = GetOverlappedResult(self->handle, &self->overlapped, &transferred, wait != 0); @@ -185,8 +258,34 @@ return Py_BuildValue("II", (unsigned) transferred, (unsigned) err); } +/*[clinic input] +_winapi.Overlapped.getbuffer + + self: 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 +299,34 @@ return res; } +/*[clinic input] +_winapi.Overlapped.cancel + + self: 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 +347,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 +423,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,28 +474,57 @@ Py_RETURN_NONE; } +/*[clinic input] +_winapi.ConnectNamedPipe + + handle: HANDLE + overlapped: bool = False +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_ConnectNamedPipe__doc__, +"ConnectNamedPipe(handle, overlapped=False)"); + +#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 "|p: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=1a8fdb5aa898a16927f3460cd245c62df3453ea9]*/ +{ + int use_overlapped = overlapped; + { BOOL success; OverlappedObject *overlapped = NULL; - static char *kwlist[] = {"handle", "overlapped", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, - F_HANDLE "|" F_BOOL, kwlist, - &hNamedPipe, &use_overlapped)) - return NULL; if (use_overlapped) { - overlapped = new_overlapped(hNamedPipe); + overlapped = new_overlapped(handle); if (!overlapped) return NULL; } Py_BEGIN_ALLOW_THREADS - success = ConnectNamedPipe(hNamedPipe, + success = ConnectNamedPipe(handle, overlapped ? &overlapped->overlapped : NULL); Py_END_ALLOW_THREADS @@ -366,96 +546,202 @@ return PyErr_SetFromWindowsErr(0); Py_RETURN_NONE; + } +} + +/*[clinic input] +_winapi.CreateFile -> HANDLE + + file_name: LPCTSTR + desired_access: DWORD + share_mode: DWORD + security_attributes: LPSECURITY_ATTRIBUTES + creation_disposition: DWORD + flags_and_attributes: DWORD + template_file: HANDLE + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_CreateFile__doc__, +"CreateFile(file_name, desired_access, share_mode, security_attributes, creation_disposition, flags_and_attributes, template_file)"); + +#define _WINAPI_CREATEFILE_METHODDEF \ + {"CreateFile", (PyCFunction)_winapi_CreateFile, METH_VARARGS, _winapi_CreateFile__doc__}, + +static HANDLE +_winapi_CreateFile_impl(PyModuleDef *module, LPCTSTR file_name, DWORD desired_access, DWORD share_mode, LPSECURITY_ATTRIBUTES security_attributes, DWORD creation_disposition, DWORD flags_and_attributes, HANDLE template_file); + +static PyObject * +_winapi_CreateFile(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + LPCTSTR file_name; + DWORD desired_access; + DWORD share_mode; + LPSECURITY_ATTRIBUTES security_attributes; + DWORD creation_disposition; + DWORD flags_and_attributes; + HANDLE template_file; + HANDLE _return_value; + + if (!PyArg_ParseTuple(args, + "skk" F_POINTER "kk" F_HANDLE ":CreateFile", + &file_name, &desired_access, &share_mode, &security_attributes, &creation_disposition, &flags_and_attributes, &template_file)) + goto exit; + _return_value = _winapi_CreateFile_impl(module, file_name, desired_access, share_mode, security_attributes, creation_disposition, flags_and_attributes, template_file); + if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred()) + goto exit; + if (_return_value == NULL) + Py_RETURN_NONE; + return_value = HANDLE_TO_PYNUM(_return_value); + +exit: + return return_value; +} + +static HANDLE +_winapi_CreateFile_impl(PyModuleDef *module, LPCTSTR file_name, DWORD desired_access, DWORD share_mode, LPSECURITY_ATTRIBUTES security_attributes, DWORD creation_disposition, DWORD flags_and_attributes, HANDLE template_file) +/*[clinic end generated code: checksum=c3beff5d0968c328d4847f6890f5d2b3a4046661]*/ +{ + HANDLE handle; + + Py_BEGIN_ALLOW_THREADS + handle = CreateFile(file_name, desired_access, + share_mode, security_attributes, + creation_disposition, + flags_and_attributes, template_file); + Py_END_ALLOW_THREADS + + if (handle == INVALID_HANDLE_VALUE) + PyErr_SetFromWindowsErr(0); + + return handle; +} + +/*[clinic input] +_winapi.CreateNamedPipe -> HANDLE + + name: LPCTSTR + open_mode: DWORD + pipe_mode: DWORD + max_instances: DWORD + out_buffer_size: DWORD + in_buffer_size: DWORD + default_timeout: DWORD + security_attributes: LPSECURITY_ATTRIBUTES + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_CreateNamedPipe__doc__, +"CreateNamedPipe(name, open_mode, pipe_mode, max_instances, out_buffer_size, in_buffer_size, default_timeout, security_attributes)"); + +#define _WINAPI_CREATENAMEDPIPE_METHODDEF \ + {"CreateNamedPipe", (PyCFunction)_winapi_CreateNamedPipe, METH_VARARGS, _winapi_CreateNamedPipe__doc__}, + +static HANDLE +_winapi_CreateNamedPipe_impl(PyModuleDef *module, LPCTSTR name, DWORD open_mode, DWORD pipe_mode, DWORD max_instances, DWORD out_buffer_size, DWORD in_buffer_size, DWORD default_timeout, LPSECURITY_ATTRIBUTES security_attributes); + +static PyObject * +_winapi_CreateNamedPipe(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + LPCTSTR name; + DWORD open_mode; + DWORD pipe_mode; + DWORD max_instances; + DWORD out_buffer_size; + DWORD in_buffer_size; + DWORD default_timeout; + LPSECURITY_ATTRIBUTES security_attributes; + HANDLE _return_value; + + if (!PyArg_ParseTuple(args, + "skkkkkk" F_POINTER ":CreateNamedPipe", + &name, &open_mode, &pipe_mode, &max_instances, &out_buffer_size, &in_buffer_size, &default_timeout, &security_attributes)) + goto exit; + _return_value = _winapi_CreateNamedPipe_impl(module, name, open_mode, pipe_mode, max_instances, out_buffer_size, in_buffer_size, default_timeout, security_attributes); + if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred()) + goto exit; + if (_return_value == NULL) + Py_RETURN_NONE; + return_value = HANDLE_TO_PYNUM(_return_value); + +exit: + return return_value; +} + +static HANDLE +_winapi_CreateNamedPipe_impl(PyModuleDef *module, LPCTSTR name, DWORD open_mode, DWORD pipe_mode, DWORD max_instances, DWORD out_buffer_size, DWORD in_buffer_size, DWORD default_timeout, LPSECURITY_ATTRIBUTES security_attributes) +/*[clinic end generated code: checksum=8b2354dfdf63c0dc944fa61e002109ae502e1ad3]*/ +{ + HANDLE handle; + + Py_BEGIN_ALLOW_THREADS + handle = CreateNamedPipe(name, open_mode, pipe_mode, + max_instances, out_buffer_size, + in_buffer_size, default_timeout, + security_attributes); + Py_END_ALLOW_THREADS + + if (handle == INVALID_HANDLE_VALUE) + PyErr_SetFromWindowsErr(0); + + return handle; +} + +/*[clinic input] +_winapi.CreatePipe + + pipe_attrs: object + Ignored internally, can be None. + size: DWORD + / + +Create an anonymous pipe. + +Returns a 2-tuple of 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 a 2-tuple of 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_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_CreateFile(PyObject *self, PyObject *args) -{ - LPCTSTR lpFileName; - DWORD dwDesiredAccess; - DWORD dwShareMode; - LPSECURITY_ATTRIBUTES lpSecurityAttributes; - DWORD dwCreationDisposition; - DWORD dwFlagsAndAttributes; - HANDLE hTemplateFile; - 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, - dwCreationDisposition, - dwFlagsAndAttributes, hTemplateFile); - Py_END_ALLOW_THREADS - - if (handle == INVALID_HANDLE_VALUE) - return PyErr_SetFromWindowsErr(0); - - return Py_BuildValue(F_HANDLE, handle); -} - -static PyObject * -winapi_CreateNamedPipe(PyObject *self, PyObject *args) -{ - LPCTSTR lpName; - DWORD dwOpenMode; - DWORD dwPipeMode; - DWORD nMaxInstances; - DWORD nOutBufferSize; - DWORD nInBufferSize; - DWORD nDefaultTimeOut; - LPSECURITY_ATTRIBUTES lpSecurityAttributes; - 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, - nInBufferSize, nDefaultTimeOut, - lpSecurityAttributes); - Py_END_ALLOW_THREADS - - if (handle == INVALID_HANDLE_VALUE) - return PyErr_SetFromWindowsErr(0); - - 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."); - -static PyObject * -winapi_CreatePipe(PyObject* self, PyObject* args) +_winapi_CreatePipe_impl(PyModuleDef *module, PyObject *pipe_attrs, DWORD size) +/*[clinic end generated code: checksum=3605a09b6197ffc39a1ef2e80599b6f6154f09fb]*/ { 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 +862,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 +936,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,40 +989,71 @@ 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 -> HANDLE + + 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 HANDLE +_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(PyObject* self, PyObject* args) +_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; DWORD desired_access; BOOL inherit_handle; DWORD options = 0; + HANDLE _return_value; - 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); + if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred()) + goto exit; + if (_return_value == NULL) + Py_RETURN_NONE; + return_value = HANDLE_TO_PYNUM(_return_value); + +exit: + return return_value; +} + +static HANDLE +_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=206d48bd590edbe448a78e692375c87e6e58aab5]*/ +{ + HANDLE target_handle; + BOOL result; Py_BEGIN_ALLOW_THREADS result = DuplicateHandle( @@ -719,60 +1067,144 @@ ); Py_END_ALLOW_THREADS - if (! result) - return PyErr_SetFromWindowsErr(GetLastError()); + if (! result) { + PyErr_SetFromWindowsErr(GetLastError()); + return INVALID_HANDLE_VALUE; + } - return HANDLE_TO_PYNUM(target_handle); + return 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_impl(PyModuleDef *module, UINT ExitCode); + +static PyObject * +_winapi_ExitProcess(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + UINT ExitCode; + + if (!PyArg_ParseTuple(args, + "I:ExitProcess", + &ExitCode)) + goto exit; + return_value = _winapi_ExitProcess_impl(module, ExitCode); + +exit: + return return_value; } static PyObject * -winapi_ExitProcess(PyObject *self, PyObject *args) +_winapi_ExitProcess_impl(PyModuleDef *module, UINT ExitCode) +/*[clinic end generated code: checksum=0706182c4e3168bedef8dc03fad247b8d7f2258c]*/ { - UINT uExitCode; - - if (!PyArg_ParseTuple(args, F_UINT, &uExitCode)) - return NULL; - #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 -> HANDLE + +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 HANDLE +_winapi_GetCurrentProcess_impl(PyModuleDef *module); static PyObject * -winapi_GetCurrentProcess(PyObject* self, PyObject* args) +_winapi_GetCurrentProcess(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) { - if (! PyArg_ParseTuple(args, ":GetCurrentProcess")) - return NULL; + PyObject *return_value = NULL; + HANDLE _return_value; - return HANDLE_TO_PYNUM(GetCurrentProcess()); + _return_value = _winapi_GetCurrentProcess_impl(module); + if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred()) + goto exit; + if (_return_value == NULL) + Py_RETURN_NONE; + return_value = HANDLE_TO_PYNUM(_return_value); + +exit: + return return_value; } -PyDoc_STRVAR(GetExitCodeProcess_doc, -"GetExitCodeProcess(handle) -> Exit code\n\ -\n\ -Return the termination status of the specified process."); +static HANDLE +_winapi_GetCurrentProcess_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=dd18f1e58864020916de772447d3b77fd7fb3f17]*/ +{ + return GetCurrentProcess(); +} + +/*[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 +1213,95 @@ return PyLong_FromUnsignedLong(exit_code); } +/*[clinic input] +_winapi.GetLastError -> DWORD +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_GetLastError__doc__, +"GetLastError()"); + +#define _WINAPI_GETLASTERROR_METHODDEF \ + {"GetLastError", (PyCFunction)_winapi_GetLastError, METH_NOARGS, _winapi_GetLastError__doc__}, + +static DWORD +_winapi_GetLastError_impl(PyModuleDef *module); + static PyObject * -winapi_GetLastError(PyObject *self, PyObject *args) +_winapi_GetLastError(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) { - return Py_BuildValue(F_DWORD, GetLastError()); + PyObject *return_value = NULL; + DWORD _return_value; + + _return_value = _winapi_GetLastError_impl(module); + return_value = Py_BuildValue("k", _return_value); + + return return_value; } -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."); +static DWORD +_winapi_GetLastError_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=92a3188aad220be155990d9462c7f299e8d2ef0a]*/ +{ + return GetLastError(); +} + +/*[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,83 +1310,200 @@ 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 -> HANDLE + + 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 HANDLE +_winapi_GetStdHandle_impl(PyModuleDef *module, DWORD std_handle); static PyObject * -winapi_GetStdHandle(PyObject* self, PyObject* args) +_winapi_GetStdHandle(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + DWORD std_handle; + HANDLE _return_value; + + if (!PyArg_ParseTuple(args, + "k:GetStdHandle", + &std_handle)) + goto exit; + _return_value = _winapi_GetStdHandle_impl(module, std_handle); + if ((_return_value == INVALID_HANDLE_VALUE) && PyErr_Occurred()) + goto exit; + if (_return_value == NULL) + Py_RETURN_NONE; + return_value = HANDLE_TO_PYNUM(_return_value); + +exit: + return return_value; +} + +static HANDLE +_winapi_GetStdHandle_impl(PyModuleDef *module, DWORD std_handle) +/*[clinic end generated code: checksum=93faba74d1e6ff92d7b7d8de48539d9c1f9e1fc3]*/ { HANDLE handle; - DWORD std_handle; - - if (! PyArg_ParseTuple(args, F_DWORD ":GetStdHandle", &std_handle)) - return NULL; Py_BEGIN_ALLOW_THREADS handle = GetStdHandle(std_handle); Py_END_ALLOW_THREADS if (handle == INVALID_HANDLE_VALUE) - return PyErr_SetFromWindowsErr(GetLastError()); + PyErr_SetFromWindowsErr(GetLastError()); - if (! handle) { - Py_INCREF(Py_None); - return Py_None; - } - - /* note: returns integer, not handle object */ - return HANDLE_TO_PYNUM(handle); + return handle; } -PyDoc_STRVAR(GetVersion_doc, -"GetVersion() -> version\n\ -\n\ -Return the version number of the current operating system."); +/*[clinic input] +_winapi.GetVersion -> long + +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 long +_winapi_GetVersion_impl(PyModuleDef *module); static PyObject * -winapi_GetVersion(PyObject* self, PyObject* args) +_winapi_GetVersion(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) { - if (! PyArg_ParseTuple(args, ":GetVersion")) - return NULL; + PyObject *return_value = NULL; + long _return_value; - return PyLong_FromUnsignedLong(GetVersion()); + _return_value = _winapi_GetVersion_impl(module); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyLong_FromLong(_return_value); + +exit: + return return_value; +} + +static long +_winapi_GetVersion_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=06defab258fd3d63874ac2a02292d90ae96d6602]*/ +{ + return GetVersion(); +} + +/*[clinic input] +_winapi.OpenProcess + + desired_access: DWORD + inherit_handle: BOOL + process_id: DWORD + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_OpenProcess__doc__, +"OpenProcess(desired_access, inherit_handle, process_id)"); + +#define _WINAPI_OPENPROCESS_METHODDEF \ + {"OpenProcess", (PyCFunction)_winapi_OpenProcess, METH_VARARGS, _winapi_OpenProcess__doc__}, + +static PyObject * +_winapi_OpenProcess_impl(PyModuleDef *module, DWORD desired_access, BOOL inherit_handle, DWORD process_id); + +static PyObject * +_winapi_OpenProcess(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + DWORD desired_access; + BOOL inherit_handle; + DWORD process_id; + + if (!PyArg_ParseTuple(args, + "kik:OpenProcess", + &desired_access, &inherit_handle, &process_id)) + goto exit; + return_value = _winapi_OpenProcess_impl(module, desired_access, inherit_handle, process_id); + +exit: + return return_value; } static PyObject * -winapi_OpenProcess(PyObject *self, PyObject *args) +_winapi_OpenProcess_impl(PyModuleDef *module, DWORD desired_access, BOOL inherit_handle, DWORD process_id) +/*[clinic end generated code: checksum=193c99005a5f7efdfbe8e640b2a0fcd7a01f4f77]*/ { - DWORD dwDesiredAccess; - BOOL bInheritHandle; - DWORD dwProcessId; HANDLE handle; - if (!PyArg_ParseTuple(args, F_DWORD F_BOOL F_DWORD, - &dwDesiredAccess, &bInheritHandle, &dwProcessId)) - return NULL; - - handle = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); + handle = OpenProcess(desired_access, inherit_handle, process_id); if (handle == NULL) return PyErr_SetFromWindowsErr(0); 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,23 +1536,53 @@ } } +/*[clinic input] +_winapi.ReadFile + + handle: HANDLE + size: int + overlapped: bool = False +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_ReadFile__doc__, +"ReadFile(handle, size, overlapped=False)"); + +#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|p: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=509b8d0c338d50717ef8cb4dd4110341ad7d6cc1]*/ +{ + int use_overlapped = overlapped; + { 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; buf = PyBytes_FromStringAndSize(NULL, size); if (!buf) @@ -985,20 +1623,55 @@ if (_PyBytes_Resize(&buf, nread)) return NULL; return Py_BuildValue("NI", buf, err); + } +} + +/*[clinic input] +_winapi.SetNamedPipeHandleState + + named_pipe: HANDLE + arg1: object + arg2: object + arg3: object + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_SetNamedPipeHandleState__doc__, +"SetNamedPipeHandleState(named_pipe, arg1, arg2, arg3)"); + +#define _WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF \ + {"SetNamedPipeHandleState", (PyCFunction)_winapi_SetNamedPipeHandleState, METH_VARARGS, _winapi_SetNamedPipeHandleState__doc__}, + +static PyObject * +_winapi_SetNamedPipeHandleState_impl(PyModuleDef *module, HANDLE named_pipe, PyObject *arg1, PyObject *arg2, PyObject *arg3); + +static PyObject * +_winapi_SetNamedPipeHandleState(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + HANDLE named_pipe; + PyObject *arg1; + PyObject *arg2; + PyObject *arg3; + + if (!PyArg_ParseTuple(args, + "" F_HANDLE "OOO:SetNamedPipeHandleState", + &named_pipe, &arg1, &arg2, &arg3)) + goto exit; + return_value = _winapi_SetNamedPipeHandleState_impl(module, named_pipe, arg1, arg2, arg3); + +exit: + return return_value; } static PyObject * -winapi_SetNamedPipeHandleState(PyObject *self, PyObject *args) +_winapi_SetNamedPipeHandleState_impl(PyModuleDef *module, HANDLE named_pipe, PyObject *arg1, PyObject *arg2, PyObject *arg3) +/*[clinic end generated code: checksum=3966cae147d4895b21172a7023d832e2c08af2a9]*/ { - HANDLE hNamedPipe; - PyObject *oArgs[3]; + 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 +1683,57 @@ } } - if (!SetNamedPipeHandleState(hNamedPipe, pArgs[0], pArgs[1], pArgs[2])) + if (!SetNamedPipeHandleState(named_pipe, 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 +1742,48 @@ return Py_None; } +/*[clinic input] +_winapi.WaitNamedPipe + + name: LPCTSTR + timeout: DWORD + / +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_WaitNamedPipe__doc__, +"WaitNamedPipe(name, 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 name, DWORD timeout); + +static PyObject * +_winapi_WaitNamedPipe(PyModuleDef *module, PyObject *args) { - LPCTSTR lpNamedPipeName; - DWORD nTimeOut; + PyObject *return_value = NULL; + LPCTSTR name; + DWORD timeout; + + if (!PyArg_ParseTuple(args, + "sk:WaitNamedPipe", + &name, &timeout)) + goto exit; + return_value = _winapi_WaitNamedPipe_impl(module, name, timeout); + +exit: + return return_value; +} + +static PyObject * +_winapi_WaitNamedPipe_impl(PyModuleDef *module, LPCTSTR name, DWORD timeout) +/*[clinic end generated code: checksum=e9af50bf530b00537d7b2af1d9d79237a483cee7]*/ +{ BOOL success; - if (!PyArg_ParseTuple(args, "s" F_DWORD, &lpNamedPipeName, &nTimeOut)) - return NULL; - Py_BEGIN_ALLOW_THREADS - success = WaitNamedPipe(lpNamedPipeName, nTimeOut); + success = WaitNamedPipe(name, timeout); Py_END_ALLOW_THREADS if (!success) @@ -1061,21 +1792,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,53 +1889,120 @@ 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 -> long + + 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 long +_winapi_WaitForSingleObject_impl(PyModuleDef *module, HANDLE handle, DWORD milliseconds); static PyObject * -winapi_WaitForSingleObject(PyObject* self, PyObject* args) +_winapi_WaitForSingleObject(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + HANDLE handle; + DWORD milliseconds; + long _return_value; + + if (!PyArg_ParseTuple(args, + "" F_HANDLE "k:WaitForSingleObject", + &handle, &milliseconds)) + goto exit; + _return_value = _winapi_WaitForSingleObject_impl(module, handle, milliseconds); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyLong_FromLong(_return_value); + +exit: + return return_value; +} + +static long +_winapi_WaitForSingleObject_impl(PyModuleDef *module, HANDLE handle, DWORD milliseconds) +/*[clinic end generated code: checksum=12d4cf89457a804151c39ddb8335067f22dd6d1e]*/ { 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 - if (result == WAIT_FAILED) - return PyErr_SetFromWindowsErr(GetLastError()); + if (result == WAIT_FAILED) { + PyErr_SetFromWindowsErr(GetLastError()); + return -1; + } - return PyLong_FromUnsignedLong(result); + return result; +} + +/*[clinic input] +_winapi.WriteFile + + handle: HANDLE + buffer: object + overlapped: bool = False +[clinic start generated code]*/ + +PyDoc_STRVAR(_winapi_WriteFile__doc__, +"WriteFile(handle, buffer, overlapped=False)"); + +#define _WINAPI_WRITEFILE_METHODDEF \ + {"WriteFile", (PyCFunction)_winapi_WriteFile, METH_VARARGS|METH_KEYWORDS, _winapi_WriteFile__doc__}, + +static PyObject * +_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|p:WriteFile", _keywords, + &handle, &buffer, &overlapped)) + goto exit; + return_value = _winapi_WriteFile_impl(module, handle, buffer, overlapped); + +exit: + return return_value; } static PyObject * -winapi_WriteFile(PyObject *self, PyObject *args, PyObject *kwds) +_winapi_WriteFile_impl(PyModuleDef *module, HANDLE handle, PyObject *buffer, int overlapped) +/*[clinic end generated code: checksum=e00a73179ec915bd264a8632905c884f0151d29a]*/ { - HANDLE handle; + int use_overlapped = overlapped; + { 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}; - - /* 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); @@ -1186,7 +2013,7 @@ else buf = &_buf; - if (!PyArg_Parse(bufobj, "y*", buf)) { + if (!PyArg_Parse(buffer, "y*", buf)) { Py_XDECREF(overlapped); return NULL; } @@ -1215,56 +2042,34 @@ if (!ret) return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); return Py_BuildValue("II", written, err); + } } 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} }; diff -r cd3fdf21a6e4 -r 8cf882d09d3f PC/msvcrtmodule.c --- a/PC/msvcrtmodule.c Thu Jan 16 14:15:03 2014 -0800 +++ b/PC/msvcrtmodule.c Fri Jan 17 01:38:15 2014 -0600 @@ -32,167 +32,408 @@ #endif #endif -// Force the malloc heap to clean itself up, and free unused blocks -// back to the OS. (According to the docs, only works on NT.) +/*[clinic input] +module msvcrt +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + +/*[clinic input] +msvcrt.heapmin + +Minimize the malloc() heap. + +Force the malloc() heap to clean itself up and return unused blocks +to the operating system. On failure, this raises OSError. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_heapmin__doc__, +"heapmin()\n" +"Minimize the malloc() heap.\n" +"\n" +"Force the malloc() heap to clean itself up and return unused blocks\n" +"to the operating system. On failure, this raises OSError."); + +#define MSVCRT_HEAPMIN_METHODDEF \ + {"heapmin", (PyCFunction)msvcrt_heapmin, METH_NOARGS, msvcrt_heapmin__doc__}, + static PyObject * -msvcrt_heapmin(PyObject *self, PyObject *args) +msvcrt_heapmin_impl(PyModuleDef *module); + +static PyObject * +msvcrt_heapmin(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) { - if (!PyArg_ParseTuple(args, ":heapmin")) - return NULL; + PyObject *return_value = NULL; + return_value = msvcrt_heapmin_impl(module); + + return return_value; +} + +static PyObject * +msvcrt_heapmin_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=d0c9471b525728727f51b70ba2d7fdb07619902d]*/ +{ if (_heapmin() != 0) return PyErr_SetFromErrno(PyExc_IOError); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +msvcrt.locking -PyDoc_STRVAR(heapmin_doc, -"heapmin() -> None\n\ -\n\ -Force the malloc() heap to clean itself up and return unused blocks\n\ -to the operating system. On failure, this raises IOError."); + fd: int + mode: int + nbytes: long + / -// Perform locking operations on a C runtime file descriptor. +Lock part of a file based on file descriptor fd from the C runtime. + +Raises IOError on failure. The locked region of the file extends from +the current file position for nbytes bytes, and may continue beyond +the end of the file. mode must be one of the LK_* constants listed +below. Multiple regions in a file may be locked at the same time, but +may not overlap. Adjacent regions are not merged; they must be unlocked +individually. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_locking__doc__, +"locking(fd, mode, nbytes)\n" +"Lock part of a file based on file descriptor fd from the C runtime.\n" +"\n" +"Raises IOError on failure. The locked region of the file extends from\n" +"the current file position for nbytes bytes, and may continue beyond\n" +"the end of the file. mode must be one of the LK_* constants listed\n" +"below. Multiple regions in a file may be locked at the same time, but\n" +"may not overlap. Adjacent regions are not merged; they must be unlocked\n" +"individually."); + +#define MSVCRT_LOCKING_METHODDEF \ + {"locking", (PyCFunction)msvcrt_locking, METH_VARARGS, msvcrt_locking__doc__}, + static PyObject * -msvcrt_locking(PyObject *self, PyObject *args) +msvcrt_locking_impl(PyModuleDef *module, int fd, int mode, long nbytes); + +static PyObject * +msvcrt_locking(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; int fd; int mode; long nbytes; + + if (!PyArg_ParseTuple(args, + "iil:locking", + &fd, &mode, &nbytes)) + goto exit; + return_value = msvcrt_locking_impl(module, fd, mode, nbytes); + +exit: + return return_value; +} + +static PyObject * +msvcrt_locking_impl(PyModuleDef *module, int fd, int mode, long nbytes) +/*[clinic end generated code: checksum=c6f0c14e56c9c3b2df85469036d47166d39a2665]*/ +{ 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; + Py_RETURN_NONE; } -PyDoc_STRVAR(locking_doc, -"locking(fd, mode, nbytes) -> None\n\ -\n\ -Lock part of a file based on file descriptor fd from the C runtime.\n\ -Raises IOError on failure. The locked region of the file extends from\n\ -the current file position for nbytes bytes, and may continue beyond\n\ -the end of the file. mode must be one of the LK_* constants listed\n\ -below. Multiple regions in a file may be locked at the same time, but\n\ -may not overlap. Adjacent regions are not merged; they must be unlocked\n\ -individually."); +/*[clinic input] +msvcrt.setmode -> long -// Set the file translation mode for a C runtime file descriptor. + fd: int + mode: int + / + +Set the line-end translation mode for the file descriptor fd. + +To set it to text mode, flags should be os.O_TEXT; for binary, it +should be os.O_BINARY. + +Return value is the previous mode. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_setmode__doc__, +"setmode(fd, mode)\n" +"Set the line-end translation mode for the file descriptor fd.\n" +"\n" +"To set it to text mode, flags should be os.O_TEXT; for binary, it\n" +"should be os.O_BINARY.\n" +"\n" +"Return value is the previous mode."); + +#define MSVCRT_SETMODE_METHODDEF \ + {"setmode", (PyCFunction)msvcrt_setmode, METH_VARARGS, msvcrt_setmode__doc__}, + +static long +msvcrt_setmode_impl(PyModuleDef *module, int fd, int mode); + static PyObject * -msvcrt_setmode(PyObject *self, PyObject *args) +msvcrt_setmode(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + int fd; + int mode; + long _return_value; + + if (!PyArg_ParseTuple(args, + "ii:setmode", + &fd, &mode)) + goto exit; + _return_value = msvcrt_setmode_impl(module, fd, mode); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyLong_FromLong(_return_value); + +exit: + return return_value; +} + +static long +msvcrt_setmode_impl(PyModuleDef *module, int fd, int mode) +/*[clinic end generated code: checksum=d08160cc0dc8f43d4007eaa1e161158b2aeaaf75]*/ +{ + mode = _setmode(fd, mode); + if (mode == -1) + PyErr_SetFromErrno(PyExc_IOError); + + return mode; +} + +/*[python input] +class Py_intptr_t_converter(CConverter): + type = 'Py_intptr_t' + format_unit = '"_Py_PARSE_INTPTR"' +[python start generated code]*/ +/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ +/*[clinic input] +msvcrt.open_osfhandle -> long + + handle: Py_intptr_t + flags: int + / + +Create a C runtime file descriptor from the file handle handle. + +The flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY, +and os.O_TEXT. The returned file descriptor may be used as a parameter +to os.fdopen() to create a file object. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_open_osfhandle__doc__, +"open_osfhandle(handle, flags)\n" +"Create a C runtime file descriptor from the file handle handle.\n" +"\n" +"The flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,\n" +"and os.O_TEXT. The returned file descriptor may be used as a parameter\n" +"to os.fdopen() to create a file object."); + +#define MSVCRT_OPEN_OSFHANDLE_METHODDEF \ + {"open_osfhandle", (PyCFunction)msvcrt_open_osfhandle, METH_VARARGS, msvcrt_open_osfhandle__doc__}, + +static long +msvcrt_open_osfhandle_impl(PyModuleDef *module, Py_intptr_t handle, int flags); + +static PyObject * +msvcrt_open_osfhandle(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_intptr_t handle; + int flags; + long _return_value; + + if (!PyArg_ParseTuple(args, + ""_Py_PARSE_INTPTR"i:open_osfhandle", + &handle, &flags)) + goto exit; + _return_value = msvcrt_open_osfhandle_impl(module, handle, flags); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyLong_FromLong(_return_value); + +exit: + return return_value; +} + +static long +msvcrt_open_osfhandle_impl(PyModuleDef *module, Py_intptr_t handle, int flags) +/*[clinic end generated code: checksum=9d7d04624cbbe6bd171d9108b663167cfcfdd3f8]*/ { 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); -} - -PyDoc_STRVAR(setmode_doc, -"setmode(fd, mode) -> Previous mode\n\ -\n\ -Set the line-end translation mode for the file descriptor fd. To set\n\ -it to text mode, flags should be os.O_TEXT; for binary, it should be\n\ -os.O_BINARY."); - -// Convert an OS file handle to a C runtime file descriptor. -static PyObject * -msvcrt_open_osfhandle(PyObject *self, PyObject *args) -{ - Py_intptr_t handle; - int flags; - int fd; - - if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:open_osfhandle", - &handle, &flags)) - return NULL; fd = _open_osfhandle(handle, flags); if (fd == -1) - return PyErr_SetFromErrno(PyExc_IOError); + PyErr_SetFromErrno(PyExc_IOError); - return PyLong_FromLong(fd); + return fd; } -PyDoc_STRVAR(open_osfhandle_doc, -"open_osfhandle(handle, flags) -> file descriptor\n\ -\n\ -Create a C runtime file descriptor from the file handle handle. The\n\ -flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,\n\ -and os.O_TEXT. The returned file descriptor may be used as a parameter\n\ -to os.fdopen() to create a file object."); +/*[python input] +class handle_return_converter(CReturnConverter): + type = 'Py_intptr_t' -// Convert a C runtime file descriptor to an OS file handle. + def render(self, function, data): + self.declare(data) + self.err_occurred_if("_return_value == -1", data) + data.return_conversion.append( + # 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 *)_return_value);\n') +[python start generated code]*/ +/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + +/*[clinic input] +msvcrt.get_osfhandle -> handle + + fd: int + / + +Return the file handle for the file descriptor fd. + +Raises IOError if fd is not recognized. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_get_osfhandle__doc__, +"get_osfhandle(fd)\n" +"Return the file handle for the file descriptor fd.\n" +"\n" +"Raises IOError if fd is not recognized."); + +#define MSVCRT_GET_OSFHANDLE_METHODDEF \ + {"get_osfhandle", (PyCFunction)msvcrt_get_osfhandle, METH_VARARGS, msvcrt_get_osfhandle__doc__}, + +static Py_intptr_t +msvcrt_get_osfhandle_impl(PyModuleDef *module, int fd); + static PyObject * -msvcrt_get_osfhandle(PyObject *self, PyObject *args) +msvcrt_get_osfhandle(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; int fd; - Py_intptr_t handle; + Py_intptr_t _return_value; - if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd)) - return NULL; + if (!PyArg_ParseTuple(args, + "i:get_osfhandle", + &fd)) + goto exit; + _return_value = msvcrt_get_osfhandle_impl(module, fd); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return PyLong_FromVoidPtr((void *)_return_value); - if (!_PyVerify_fd(fd)) - return PyErr_SetFromErrno(PyExc_IOError); - - 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); +exit: + return return_value; } -PyDoc_STRVAR(get_osfhandle_doc, -"get_osfhandle(fd) -> file handle\n\ -\n\ -Return the file handle for the file descriptor fd. Raises IOError\n\ -if fd is not recognized."); +static Py_intptr_t +msvcrt_get_osfhandle_impl(PyModuleDef *module, int fd) +/*[clinic end generated code: checksum=7d0adba75c28d59db50c93c06c8311c7dba60c28]*/ +{ + Py_intptr_t handle = -1; + + if (!_PyVerify_fd(fd)) { + PyErr_SetFromErrno(PyExc_IOError); + } + else { + handle = _get_osfhandle(fd); + if (handle == -1) + PyErr_SetFromErrno(PyExc_IOError); + } + + return handle; +} /* Console I/O */ +/*[clinic input] +msvcrt.kbhit + +Return true if a keypress is waiting to be read. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_kbhit__doc__, +"kbhit()\n" +"Return true if a keypress is waiting to be read."); + +#define MSVCRT_KBHIT_METHODDEF \ + {"kbhit", (PyCFunction)msvcrt_kbhit, METH_NOARGS, msvcrt_kbhit__doc__}, static PyObject * -msvcrt_kbhit(PyObject *self, PyObject *args) +msvcrt_kbhit_impl(PyModuleDef *module); + +static PyObject * +msvcrt_kbhit(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = msvcrt_kbhit_impl(module); + + return return_value; +} + +static PyObject * +msvcrt_kbhit_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=6f10f7881ff985925c97c7e163fddce28ae749f2]*/ { int ok; - if (!PyArg_ParseTuple(args, ":kbhit")) - return NULL; - ok = _kbhit(); return PyLong_FromLong(ok); } -PyDoc_STRVAR(kbhit_doc, -"kbhit() -> bool\n\ -\n\ -Return true if a keypress is waiting to be read."); +/*[clinic input] +msvcrt.getch + +Read a keypress and return the resulting character as a byte string. + +Nothing is echoed to the console. This call will block if a keypress is +not already available, but will not wait for Enter to be pressed. If the +pressed key was a special function key, this will return '\\000' or +'\\xe0'; the next call will return the keycode. The Control-C keypress +cannot be read with this function. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_getch__doc__, +"getch()\n" +"Read a keypress and return the resulting character as a byte string.\n" +"\n" +"Nothing is echoed to the console. This call will block if a keypress is\n" +"not already available, but will not wait for Enter to be pressed. If the\n" +"pressed key was a special function key, this will return \'\\000\' or\n" +"\'\\xe0\'; the next call will return the keycode. The Control-C keypress\n" +"cannot be read with this function."); + +#define MSVCRT_GETCH_METHODDEF \ + {"getch", (PyCFunction)msvcrt_getch, METH_NOARGS, msvcrt_getch__doc__}, static PyObject * -msvcrt_getch(PyObject *self, PyObject *args) +msvcrt_getch_impl(PyModuleDef *module); + +static PyObject * +msvcrt_getch(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = msvcrt_getch_impl(module); + + return return_value; +} + +static PyObject * +msvcrt_getch_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=e8836714d97ec61ae8a1bf6dc0b643c492b837f9]*/ { int ch; char s[1]; - if (!PyArg_ParseTuple(args, ":getch")) - return NULL; - Py_BEGIN_ALLOW_THREADS ch = _getch(); Py_END_ALLOW_THREADS @@ -200,46 +441,81 @@ return PyBytes_FromStringAndSize(s, 1); } -PyDoc_STRVAR(getch_doc, -"getch() -> key character\n\ -\n\ -Read a keypress and return the resulting character as a byte string.\n\ -Nothing is echoed to the console. This call will block if a keypress is\n\ -not already available, but will not wait for Enter to be pressed. If the\n\ -pressed key was a special function key, this will return '\\000' or\n\ -'\\xe0'; the next call will return the keycode. The Control-C keypress\n\ -cannot be read with this function."); +#ifdef _WCONIO_DEFINED +/*[clinic input] +msvcrt.getwch -#ifdef _WCONIO_DEFINED +Wide char variant of getch(), returning a Unicode value. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_getwch__doc__, +"getwch()\n" +"Wide char variant of getch(), returning a Unicode value."); + +#define MSVCRT_GETWCH_METHODDEF \ + {"getwch", (PyCFunction)msvcrt_getwch, METH_NOARGS, msvcrt_getwch__doc__}, + static PyObject * -msvcrt_getwch(PyObject *self, PyObject *args) +msvcrt_getwch_impl(PyModuleDef *module); + +static PyObject * +msvcrt_getwch(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = msvcrt_getwch_impl(module); + + return return_value; +} + +static PyObject * +msvcrt_getwch_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=a902e8ecc086f94961d630d04766bb0309789b47]*/ { wchar_t ch; - if (!PyArg_ParseTuple(args, ":getwch")) - return NULL; - Py_BEGIN_ALLOW_THREADS ch = _getwch(); Py_END_ALLOW_THREADS return PyUnicode_FromOrdinal(ch); } +#else +#define MSVCRT_GETWCH_METHODDEF +#endif /* _WCONIO_DEFINED */ -PyDoc_STRVAR(getwch_doc, -"getwch() -> Unicode key character\n\ -\n\ -Wide char variant of getch(), returning a Unicode value."); -#endif +/*[clinic input] +msvcrt.getche + +Similar to getch(), but the keypress will be echoed if possible. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_getche__doc__, +"getche()\n" +"Similar to getch(), but the keypress will be echoed if possible."); + +#define MSVCRT_GETCHE_METHODDEF \ + {"getche", (PyCFunction)msvcrt_getche, METH_NOARGS, msvcrt_getche__doc__}, static PyObject * -msvcrt_getche(PyObject *self, PyObject *args) +msvcrt_getche_impl(PyModuleDef *module); + +static PyObject * +msvcrt_getche(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = msvcrt_getche_impl(module); + + return return_value; +} + +static PyObject * +msvcrt_getche_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=e662379428dc504b687461a5d510e66c5ac95ad6]*/ { int ch; char s[1]; - if (!PyArg_ParseTuple(args, ":getche")) - return NULL; - Py_BEGIN_ALLOW_THREADS ch = _getche(); Py_END_ALLOW_THREADS @@ -247,112 +523,238 @@ return PyBytes_FromStringAndSize(s, 1); } -PyDoc_STRVAR(getche_doc, -"getche() -> key character\n\ -\n\ -Similar to getch(), but the keypress will be echoed if it represents\n\ -a printable character."); +#ifdef _WCONIO_DEFINED +/*[clinic input] +msvcrt.getwche -#ifdef _WCONIO_DEFINED +Wide char variant of getche(), returning a Unicode value. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_getwche__doc__, +"getwche()\n" +"Wide char variant of getche(), returning a Unicode value."); + +#define MSVCRT_GETWCHE_METHODDEF \ + {"getwche", (PyCFunction)msvcrt_getwche, METH_NOARGS, msvcrt_getwche__doc__}, + static PyObject * -msvcrt_getwche(PyObject *self, PyObject *args) +msvcrt_getwche_impl(PyModuleDef *module); + +static PyObject * +msvcrt_getwche(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = msvcrt_getwche_impl(module); + + return return_value; +} + +static PyObject * +msvcrt_getwche_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=8d480cf402d263dc649474af69356a9833a49724]*/ { wchar_t ch; - if (!PyArg_ParseTuple(args, ":getwche")) - return NULL; - Py_BEGIN_ALLOW_THREADS ch = _getwche(); Py_END_ALLOW_THREADS return PyUnicode_FromOrdinal(ch); } +#else +#define MSVCRT_GETWCHE_METHODDEF +#endif /* _WCONIO_DEFINED */ -PyDoc_STRVAR(getwche_doc, -"getwche() -> Unicode key character\n\ -\n\ -Wide char variant of getche(), returning a Unicode value."); -#endif +/*[clinic input] +msvcrt.putch + + char: char + / + +Print the byte string char to the console without buffering. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_putch__doc__, +"putch(char)\n" +"Print the byte string char to the console without buffering."); + +#define MSVCRT_PUTCH_METHODDEF \ + {"putch", (PyCFunction)msvcrt_putch, METH_VARARGS, msvcrt_putch__doc__}, static PyObject * -msvcrt_putch(PyObject *self, PyObject *args) +msvcrt_putch_impl(PyModuleDef *module, char char_value); + +static PyObject * +msvcrt_putch(PyModuleDef *module, PyObject *args) { - char ch; + PyObject *return_value = NULL; + char char_value; - if (!PyArg_ParseTuple(args, "c:putch", &ch)) - return NULL; + if (!PyArg_ParseTuple(args, + "c:putch", + &char_value)) + goto exit; + return_value = msvcrt_putch_impl(module, char_value); - _putch(ch); - Py_INCREF(Py_None); - return Py_None; +exit: + return return_value; } -PyDoc_STRVAR(putch_doc, -"putch(char) -> None\n\ -\n\ -Print the byte string char to the console without buffering."); +static PyObject * +msvcrt_putch_impl(PyModuleDef *module, char char_value) +/*[clinic end generated code: checksum=2fc9e33223995667ed26c251873404f3040f01e9]*/ +{ + _putch(char_value); + Py_RETURN_NONE; +} #ifdef _WCONIO_DEFINED +/*[clinic input] +msvcrt.putwch + + unicode_char: int(types='str') + / + +Wide char variant of putch(), accepting a Unicode value. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_putwch__doc__, +"putwch(unicode_char)\n" +"Wide char variant of putch(), accepting a Unicode value."); + +#define MSVCRT_PUTWCH_METHODDEF \ + {"putwch", (PyCFunction)msvcrt_putwch, METH_VARARGS, msvcrt_putwch__doc__}, + static PyObject * -msvcrt_putwch(PyObject *self, PyObject *args) +msvcrt_putwch_impl(PyModuleDef *module, int unicode_char); + +static PyObject * +msvcrt_putwch(PyModuleDef *module, PyObject *args) { - int ch; + PyObject *return_value = NULL; + int unicode_char; - if (!PyArg_ParseTuple(args, "C:putwch", &ch)) - return NULL; + if (!PyArg_ParseTuple(args, + "C:putwch", + &unicode_char)) + goto exit; + return_value = msvcrt_putwch_impl(module, unicode_char); - _putwch(ch); +exit: + return return_value; +} + +static PyObject * +msvcrt_putwch_impl(PyModuleDef *module, int unicode_char) +/*[clinic end generated code: checksum=af393175374afa21985c88255ce8fb85264f17fc]*/ +{ + _putwch(unicode_char); Py_RETURN_NONE; } +#else +#define MSVCRT_PUTWCH_METHODDEF +#endif /* _WCONIO_DEFINED */ -PyDoc_STRVAR(putwch_doc, -"putwch(unicode_char) -> None\n\ -\n\ -Wide char variant of putch(), accepting a Unicode value."); -#endif +/*[clinic input] +msvcrt.ungetch + + char: char + / + +Opposite of getch. + +Cause the byte string char to be "pushed back" into the +console buffer; it will be the next character read by +getch() or getche(). +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_ungetch__doc__, +"ungetch(char)\n" +"Opposite of getch.\n" +"\n" +"Cause the byte string char to be \"pushed back\" into the\n" +"console buffer; it will be the next character read by\n" +"getch() or getche()."); + +#define MSVCRT_UNGETCH_METHODDEF \ + {"ungetch", (PyCFunction)msvcrt_ungetch, METH_VARARGS, msvcrt_ungetch__doc__}, static PyObject * -msvcrt_ungetch(PyObject *self, PyObject *args) +msvcrt_ungetch_impl(PyModuleDef *module, char char_value); + +static PyObject * +msvcrt_ungetch(PyModuleDef *module, PyObject *args) { - char ch; + PyObject *return_value = NULL; + char char_value; - if (!PyArg_ParseTuple(args, "c:ungetch", &ch)) - return NULL; + if (!PyArg_ParseTuple(args, + "c:ungetch", + &char_value)) + goto exit; + return_value = msvcrt_ungetch_impl(module, char_value); - if (_ungetch(ch) == EOF) - return PyErr_SetFromErrno(PyExc_IOError); - Py_INCREF(Py_None); - return Py_None; +exit: + return return_value; } -PyDoc_STRVAR(ungetch_doc, -"ungetch(char) -> None\n\ -\n\ -Cause the byte string char to be \"pushed back\" into the\n\ -console buffer; it will be the next character read by\n\ -getch() or getche()."); +static PyObject * +msvcrt_ungetch_impl(PyModuleDef *module, char char_value) +/*[clinic end generated code: checksum=3d8dfc308af5b1fba22d517edc046d5bec1a9bb5]*/ +{ + if (_ungetch(char_value) == EOF) + return PyErr_SetFromErrno(PyExc_IOError); + Py_RETURN_NONE; +} #ifdef _WCONIO_DEFINED +/*[clinic input] +msvcrt.ungetwch + + unicode_char: int(types='str') + / + +Wide char variant of ungetch(), accepting a Unicode value. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_ungetwch__doc__, +"ungetwch(unicode_char)\n" +"Wide char variant of ungetch(), accepting a Unicode value."); + +#define MSVCRT_UNGETWCH_METHODDEF \ + {"ungetwch", (PyCFunction)msvcrt_ungetwch, METH_VARARGS, msvcrt_ungetwch__doc__}, + static PyObject * -msvcrt_ungetwch(PyObject *self, PyObject *args) +msvcrt_ungetwch_impl(PyModuleDef *module, int unicode_char); + +static PyObject * +msvcrt_ungetwch(PyModuleDef *module, PyObject *args) { - int ch; + PyObject *return_value = NULL; + int unicode_char; - if (!PyArg_ParseTuple(args, "C:ungetwch", &ch)) - return NULL; + if (!PyArg_ParseTuple(args, + "C:ungetwch", + &unicode_char)) + goto exit; + return_value = msvcrt_ungetwch_impl(module, unicode_char); - if (_ungetwch(ch) == WEOF) - return PyErr_SetFromErrno(PyExc_IOError); - Py_INCREF(Py_None); - return Py_None; +exit: + return return_value; } -PyDoc_STRVAR(ungetwch_doc, -"ungetwch(unicode_char) -> None\n\ -\n\ -Wide char variant of ungetch(), accepting a Unicode value."); -#endif +static PyObject * +msvcrt_ungetwch_impl(PyModuleDef *module, int unicode_char) +/*[clinic end generated code: checksum=1a2fb68c965930ff1bb524ecce42b8eb055381b2]*/ +{ + if (_ungetwch(unicode_char) == WEOF) + return PyErr_SetFromErrno(PyExc_IOError); + Py_RETURN_NONE; +} +#else +#define MSVCRT_UNGETWCH_METHODDEF +#endif /* _WCONIO_DEFINED */ static void insertint(PyObject *d, char *name, int value) @@ -370,54 +772,208 @@ #ifdef _DEBUG -static PyObject* -msvcrt_setreportfile(PyObject *self, PyObject *args) +/*[clinic input] +msvcrt.CrtSetReportFile -> long + + type: int + file: int + / + +Wrapper around _CrtSetReportFile. + +Only available on Debug builds. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_CrtSetReportFile__doc__, +"CrtSetReportFile(type, file)\n" +"Wrapper around _CrtSetReportFile.\n" +"\n" +"Only available on Debug builds."); + +#define MSVCRT_CRTSETREPORTFILE_METHODDEF \ + {"CrtSetReportFile", (PyCFunction)msvcrt_CrtSetReportFile, METH_VARARGS, msvcrt_CrtSetReportFile__doc__}, + +static long +msvcrt_CrtSetReportFile_impl(PyModuleDef *module, int type_value, int file); + +static PyObject * +msvcrt_CrtSetReportFile(PyModuleDef *module, PyObject *args) { - int type, file; - _HFILE res; + PyObject *return_value = NULL; + int type_value; + int file; + long _return_value; - 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; + if (!PyArg_ParseTuple(args, + "ii:CrtSetReportFile", + &type_value, &file)) + goto exit; + _return_value = msvcrt_CrtSetReportFile_impl(module, type_value, file); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyLong_FromLong(_return_value); + +exit: + return return_value; } -static PyObject* -msvcrt_setreportmode(PyObject *self, PyObject *args) +static long +msvcrt_CrtSetReportFile_impl(PyModuleDef *module, int type_value, int file) +/*[clinic end generated code: checksum=ca65d369a65c177ed347e38ee98428538a53d6b7]*/ { - int type, mode; + return (long)_CrtSetReportFile(type_value, (_HFILE)file); +} + +/*[clinic input] +msvcrt.CrtSetReportMode + + type: int + mode: int + / + +Wrapper around _CrtSetReportMode. + +Only available on Debug builds. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_CrtSetReportMode__doc__, +"CrtSetReportMode(type, mode)\n" +"Wrapper around _CrtSetReportMode.\n" +"\n" +"Only available on Debug builds."); + +#define MSVCRT_CRTSETREPORTMODE_METHODDEF \ + {"CrtSetReportMode", (PyCFunction)msvcrt_CrtSetReportMode, METH_VARARGS, msvcrt_CrtSetReportMode__doc__}, + +static PyObject * +msvcrt_CrtSetReportMode_impl(PyModuleDef *module, int type_value, int mode); + +static PyObject * +msvcrt_CrtSetReportMode(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + int type_value; + int mode; + + if (!PyArg_ParseTuple(args, + "ii:CrtSetReportMode", + &type_value, &mode)) + goto exit; + return_value = msvcrt_CrtSetReportMode_impl(module, type_value, mode); + +exit: + return return_value; +} + +static PyObject * +msvcrt_CrtSetReportMode_impl(PyModuleDef *module, int type_value, int mode) +/*[clinic end generated code: checksum=4c26ba9e4c8464fd4231e4786e5f65e50ea3e7e0]*/ +{ int res; - if (!PyArg_ParseTuple(args, "ii", &type, &mode)) - return NULL; - res = _CrtSetReportMode(type, mode); + res = _CrtSetReportMode(type_value, mode); if (res == -1) return PyErr_SetFromErrno(PyExc_IOError); return PyLong_FromLong(res); } -static PyObject* -msvcrt_seterrormode(PyObject *self, PyObject *args) +/*[clinic input] +msvcrt.set_error_mode -> long + + mode: int + / + +Wrapper around _set_error_mode. + +Only available on Debug builds. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_set_error_mode__doc__, +"set_error_mode(mode)\n" +"Wrapper around _set_error_mode.\n" +"\n" +"Only available on Debug builds."); + +#define MSVCRT_SET_ERROR_MODE_METHODDEF \ + {"set_error_mode", (PyCFunction)msvcrt_set_error_mode, METH_VARARGS, msvcrt_set_error_mode__doc__}, + +static long +msvcrt_set_error_mode_impl(PyModuleDef *module, int mode); + +static PyObject * +msvcrt_set_error_mode(PyModuleDef *module, PyObject *args) { - int mode, res; + PyObject *return_value = NULL; + int mode; + long _return_value; - if (!PyArg_ParseTuple(args, "i", &mode)) - return NULL; - res = _set_error_mode(mode); - return PyLong_FromLong(res); + if (!PyArg_ParseTuple(args, + "i:set_error_mode", + &mode)) + goto exit; + _return_value = msvcrt_set_error_mode_impl(module, mode); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyLong_FromLong(_return_value); + +exit: + return return_value; } -#endif +static long +msvcrt_set_error_mode_impl(PyModuleDef *module, int mode) +/*[clinic end generated code: checksum=aa541298e338886294456b05e47a011ecf35841d]*/ +{ + return _set_error_mode(mode); +} -static PyObject* -seterrormode(PyObject *self, PyObject *args) +#else +#define MSVCRT_CRTSETREPORTFILE_METHODDEF +#define MSVCRT_CRTSETREPORTMODE_METHODDEF +#define MSVCRT_SET_ERROR_MODE_METHODDEF +#endif /* _DEBUG */ + +/*[clinic input] +msvcrt.SetErrorMode + + mode: unsigned_int(bitwise=True) + / + +Wrapper around SetErrorMode. +[clinic start generated code]*/ + +PyDoc_STRVAR(msvcrt_SetErrorMode__doc__, +"SetErrorMode(mode)\n" +"Wrapper around SetErrorMode."); + +#define MSVCRT_SETERRORMODE_METHODDEF \ + {"SetErrorMode", (PyCFunction)msvcrt_SetErrorMode, METH_VARARGS, msvcrt_SetErrorMode__doc__}, + +static PyObject * +msvcrt_SetErrorMode_impl(PyModuleDef *module, unsigned int mode); + +static PyObject * +msvcrt_SetErrorMode(PyModuleDef *module, PyObject *args) { - unsigned int mode, res; + PyObject *return_value = NULL; + unsigned int mode; - if (!PyArg_ParseTuple(args, "I", &mode)) - return NULL; + if (!PyArg_ParseTuple(args, + "I:SetErrorMode", + &mode)) + goto exit; + return_value = msvcrt_SetErrorMode_impl(module, mode); + +exit: + return return_value; +} + +static PyObject * +msvcrt_SetErrorMode_impl(PyModuleDef *module, unsigned int mode) +/*[clinic end generated code: checksum=3161391324bb6ea19d64dc3eea0dad6708421553]*/ +{ + unsigned int res; + res = SetErrorMode(mode); return PyLong_FromUnsignedLong(res); } @@ -425,28 +981,24 @@ /* List of functions exported by this module */ static struct PyMethodDef msvcrt_functions[] = { - {"heapmin", msvcrt_heapmin, METH_VARARGS, heapmin_doc}, - {"locking", msvcrt_locking, METH_VARARGS, locking_doc}, - {"setmode", msvcrt_setmode, METH_VARARGS, setmode_doc}, - {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS, open_osfhandle_doc}, - {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS, get_osfhandle_doc}, - {"kbhit", msvcrt_kbhit, METH_VARARGS, kbhit_doc}, - {"getch", msvcrt_getch, METH_VARARGS, getch_doc}, - {"getche", msvcrt_getche, METH_VARARGS, getche_doc}, - {"putch", msvcrt_putch, METH_VARARGS, putch_doc}, - {"ungetch", msvcrt_ungetch, METH_VARARGS, ungetch_doc}, - {"SetErrorMode", seterrormode, METH_VARARGS}, -#ifdef _DEBUG - {"CrtSetReportFile", msvcrt_setreportfile, METH_VARARGS}, - {"CrtSetReportMode", msvcrt_setreportmode, METH_VARARGS}, - {"set_error_mode", msvcrt_seterrormode, METH_VARARGS}, -#endif -#ifdef _WCONIO_DEFINED - {"getwch", msvcrt_getwch, METH_VARARGS, getwch_doc}, - {"getwche", msvcrt_getwche, METH_VARARGS, getwche_doc}, - {"putwch", msvcrt_putwch, METH_VARARGS, putwch_doc}, - {"ungetwch", msvcrt_ungetwch, METH_VARARGS, ungetwch_doc}, -#endif + MSVCRT_HEAPMIN_METHODDEF + MSVCRT_LOCKING_METHODDEF + MSVCRT_SETMODE_METHODDEF + MSVCRT_OPEN_OSFHANDLE_METHODDEF + MSVCRT_GET_OSFHANDLE_METHODDEF + MSVCRT_KBHIT_METHODDEF + MSVCRT_GETCH_METHODDEF + MSVCRT_GETCHE_METHODDEF + MSVCRT_PUTCH_METHODDEF + MSVCRT_UNGETCH_METHODDEF + MSVCRT_SETERRORMODE_METHODDEF + MSVCRT_CRTSETREPORTFILE_METHODDEF + MSVCRT_CRTSETREPORTMODE_METHODDEF + MSVCRT_SET_ERROR_MODE_METHODDEF + MSVCRT_GETWCH_METHODDEF + MSVCRT_GETWCHE_METHODDEF + MSVCRT_PUTWCH_METHODDEF + MSVCRT_UNGETWCH_METHODDEF {NULL, NULL} }; diff -r cd3fdf21a6e4 -r 8cf882d09d3f PC/winreg.c --- a/PC/winreg.c Thu Jan 16 14:15:03 2014 -0800 +++ b/PC/winreg.c Fri Jan 17 01:38:15 2014 -0600 @@ -12,11 +12,29 @@ */ +/*[clinic input] +module winreg +class winreg.HKEYType +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + +/*[python input] +class REGSAM_converter(int_converter): type = 'REGSAM' + +class DWORD_converter(int_converter): type = 'DWORD' + +class HKEY_converter(CConverter): + type = 'HKEY' + converter = 'clinic_HKEY_converter' +[python start generated code]*/ +/*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + #include "Python.h" #include "structmember.h" #include "windows.h" static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK); +static BOOL clinic_HKEY_converter(PyObject *ob, void *p); static PyObject *PyHKEY_FromHKEY(HKEY h); static BOOL PyHKEY_Close(PyObject *obHandle); @@ -46,10 +64,12 @@ "DeleteValue() - Removes a named value from the specified registry key.\n" "EnumKey() - Enumerates subkeys of the specified open registry key.\n" "EnumValue() - Enumerates values of the specified open registry key.\n" -"ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ string.\n" +"ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ\n" +" string.\n" "FlushKey() - Writes all the attributes of the specified key to the registry.\n" -"LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and stores\n" -" registration information from a specified file into that subkey.\n" +"LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and\n" +" stores registration information from a specified file into that\n" +" subkey.\n" "OpenKey() - Opens the specified key.\n" "OpenKeyEx() - Alias of OpenKey().\n" "QueryValue() - Retrieves the value associated with the unnamed value for a\n" @@ -71,300 +91,6 @@ "to see what constants are used, and where."); -PyDoc_STRVAR(CloseKey_doc, -"CloseKey(hkey)\n" -"Closes a previously opened registry key.\n" -"\n" -"The hkey argument specifies a previously opened key.\n" -"\n" -"Note that if the key is not closed using this method, it will be\n" -"closed when the hkey object is destroyed by Python."); - -PyDoc_STRVAR(ConnectRegistry_doc, -"ConnectRegistry(computer_name, key) -> key\n" -"Establishes a connection to a predefined registry handle on another computer.\n" -"\n" -"computer_name is the name of the remote computer, of the form \\\\computername.\n" -" If None, the local computer is used.\n" -"key is the predefined handle to connect to.\n" -"\n" -"The return value is the handle of the opened key.\n" -"If the function fails, an OSError exception is raised."); - -PyDoc_STRVAR(CreateKey_doc, -"CreateKey(key, sub_key) -> key\n" -"Creates or opens the specified key.\n" -"\n" -"key is an already open key, or one of the predefined HKEY_* constants.\n" -"sub_key is a string that names the key this method opens or creates.\n" -"\n" -"If key is one of the predefined keys, sub_key may be None. In that case,\n" -"the handle returned is the same key handle passed in to the function.\n" -"\n" -"If the key already exists, this function opens the existing key.\n" -"\n" -"The return value is the handle of the opened key.\n" -"If the function fails, an OSError exception is raised."); - -PyDoc_STRVAR(CreateKeyEx_doc, -"CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE) -> key\n" -"Creates or opens the specified key.\n" -"\n" -"key is an already open key, or one of the predefined HKEY_* constants\n" -"sub_key is a string that names the key this method opens or creates.\n" -"reserved is a reserved integer, and must be zero. Default is zero.\n" -"access is an integer that specifies an access mask that describes the \n" -" desired security access for the key. Default is KEY_WRITE.\n" -"\n" -"If key is one of the predefined keys, sub_key may be None. In that case,\n" -"the handle returned is the same key handle passed in to the function.\n" -"\n" -"If the key already exists, this function opens the existing key\n" -"\n" -"The return value is the handle of the opened key.\n" -"If the function fails, an OSError exception is raised."); - -PyDoc_STRVAR(DeleteKey_doc, -"DeleteKey(key, sub_key)\n" -"Deletes the specified key.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"sub_key is a string that must be a subkey of the key identified by the key\n" -" parameter. This value must not be None, and the key may not have\n" -" subkeys.\n" -"\n" -"This method can not delete keys with subkeys.\n" -"\n" -"If the function succeeds, the entire key, including all of its values,\n" -"is removed. If the function fails, an OSError exception is raised."); - -PyDoc_STRVAR(DeleteKeyEx_doc, -"DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0)\n" -"Deletes the specified key (64-bit OS only).\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"sub_key is a string that must be a subkey of the key identified by the key\n" -" parameter. This value must not be None, and the key may not have\n" -" subkeys.\n" -"reserved is a reserved integer, and must be zero. Default is zero.\n" -"access is an integer that specifies an access mask that describes the \n" -" desired security access for the key. Default is KEY_WOW64_64KEY.\n" -"\n" -"This method can not delete keys with subkeys.\n" -"\n" -"If the function succeeds, the entire key, including all of its values,\n" -"is removed. If the function fails, an OSError exception is raised.\n" -"On unsupported Windows versions, NotImplementedError is raised."); - -PyDoc_STRVAR(DeleteValue_doc, -"DeleteValue(key, value)\n" -"Removes a named value from a registry key.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"value is a string that identifies the value to remove."); - -PyDoc_STRVAR(EnumKey_doc, -"EnumKey(key, index) -> string\n" -"Enumerates subkeys of an open registry key.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"index is an integer that identifies the index of the key to retrieve.\n" -"\n" -"The function retrieves the name of one subkey each time it is called.\n" -"It is typically called repeatedly until an OSError exception is\n" -"raised, indicating no more values are available."); - -PyDoc_STRVAR(EnumValue_doc, -"EnumValue(key, index) -> tuple\n" -"Enumerates values of an open registry key.\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"index is an integer that identifies the index of the value to retrieve.\n" -"\n" -"The function retrieves the name of one subkey each time it is called.\n" -"It is typically called repeatedly, until an OSError exception\n" -"is raised, indicating no more values.\n" -"\n" -"The result is a tuple of 3 items:\n" -"value_name is a string that identifies the value.\n" -"value_data is an object that holds the value data, and whose type depends\n" -" on the underlying registry type.\n" -"data_type is an integer that identifies the type of the value data."); - -PyDoc_STRVAR(ExpandEnvironmentStrings_doc, -"ExpandEnvironmentStrings(string) -> string\n" -"Expand environment vars.\n"); - -PyDoc_STRVAR(FlushKey_doc, -"FlushKey(key)\n" -"Writes all the attributes of a key to the registry.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"\n" -"It is not necessary to call FlushKey to change a key. Registry changes are\n" -"flushed to disk by the registry using its lazy flusher. Registry changes are\n" -"also flushed to disk at system shutdown. Unlike CloseKey(), the FlushKey()\n" -"method returns only when all the data has been written to the registry.\n" -"\n" -"An application should only call FlushKey() if it requires absolute certainty\n" -"that registry changes are on disk. If you don't know whether a FlushKey()\n" -"call is required, it probably isn't."); - -PyDoc_STRVAR(LoadKey_doc, -"LoadKey(key, sub_key, file_name)\n" -"Creates a subkey under the specified key and stores registration information\n" -"from a specified file into that subkey.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"sub_key is a string that identifies the sub_key to load.\n" -"file_name is the name of the file to load registry data from. This file must\n" -" have been created with the SaveKey() function. Under the file\n" -" allocation table (FAT) file system, the filename may not have an\n" -" extension.\n" -"\n" -"A call to LoadKey() fails if the calling process does not have the\n" -"SE_RESTORE_PRIVILEGE privilege.\n" -"\n" -"If key is a handle returned by ConnectRegistry(), then the path specified\n" -"in fileName is relative to the remote computer.\n" -"\n" -"The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree"); - -PyDoc_STRVAR(OpenKey_doc, -"OpenKey(key, sub_key, reserved=0, access=KEY_READ) -> key\n" -"Opens the specified key.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"sub_key is a string that identifies the sub_key to open.\n" -"reserved is a reserved integer, and must be zero. Default is zero.\n" -"access is an integer that specifies an access mask that describes the desired\n" -" security access for the key. Default is KEY_READ\n" -"\n" -"The result is a new handle to the specified key\n" -"If the function fails, an OSError exception is raised."); - -PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()"); - -PyDoc_STRVAR(QueryInfoKey_doc, -"QueryInfoKey(key) -> tuple\n" -"Returns information about a key.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"\n" -"The result is a tuple of 3 items:" -"An integer that identifies the number of sub keys this key has.\n" -"An integer that identifies the number of values this key has.\n" -"An integer that identifies when the key was last modified (if available)\n" -" as 100's of nanoseconds since Jan 1, 1600."); - -PyDoc_STRVAR(QueryValue_doc, -"QueryValue(key, sub_key) -> string\n" -"Retrieves the unnamed value for a key.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"sub_key is a string that holds the name of the subkey with which the value\n" -" is associated. If this parameter is None or empty, the function\n" -" retrieves the value set by the SetValue() method for the key\n" -" identified by key." -"\n" -"Values in the registry have name, type, and data components. This method\n" -"retrieves the data for a key's first value that has a NULL name.\n" -"But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!"); - -PyDoc_STRVAR(QueryValueEx_doc, -"QueryValueEx(key, value_name) -> (value, type_id)\n" -"Retrieves the type and data for a specified value name associated with an\n" -"open registry key.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"value_name is a string indicating the value to query"); - -PyDoc_STRVAR(SaveKey_doc, -"SaveKey(key, file_name)\n" -"Saves the specified key, and all its subkeys to the specified file.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"file_name is the name of the file to save registry data to. This file cannot\n" -" already exist. If this filename includes an extension, it cannot be\n" -" used on file allocation table (FAT) file systems by the LoadKey(),\n" -" ReplaceKey() or RestoreKey() methods.\n" -"\n" -"If key represents a key on a remote computer, the path described by file_name\n" -"is relative to the remote computer.\n" -"\n" -"The caller of this method must possess the SeBackupPrivilege security\n" -"privilege. This function passes NULL for security_attributes to the API."); - -PyDoc_STRVAR(SetValue_doc, -"SetValue(key, sub_key, type, value)\n" -"Associates a value with a specified key.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"sub_key is a string that names the subkey with which the value is associated.\n" -"type is an integer that specifies the type of the data. Currently this must\n" -" be REG_SZ, meaning only strings are supported.\n" -"value is a string that specifies the new value.\n" -"\n" -"If the key specified by the sub_key parameter does not exist, the SetValue\n" -"function creates it.\n" -"\n" -"Value lengths are limited by available memory. Long values (more than\n" -"2048 bytes) should be stored as files with the filenames stored in \n" -"the configuration registry. This helps the registry perform efficiently.\n" -"\n" -"The key identified by the key parameter must have been opened with\n" -"KEY_SET_VALUE access."); - -PyDoc_STRVAR(SetValueEx_doc, -"SetValueEx(key, value_name, reserved, type, value)\n" -"Stores data in the value field of an open registry key.\n" -"\n" -"key is an already open key, or any one of the predefined HKEY_* constants.\n" -"value_name is a string containing the name of the value to set, or None.\n" -"reserved can be anything - zero is always passed to the API.\n" -"type is an integer that specifies the type of the data. This should be one of:\n" -" REG_BINARY -- Binary data in any form.\n" -" REG_DWORD -- A 32-bit number.\n" -" REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n" -" REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n" -" REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n" -" to environment variables (for example, %PATH%).\n" -" REG_LINK -- A Unicode symbolic link.\n" -" REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n" -" two null characters. Note that Python handles this\n" -" termination automatically.\n" -" REG_NONE -- No defined value type.\n" -" REG_RESOURCE_LIST -- A device-driver resource list.\n" -" REG_SZ -- A null-terminated string.\n" -"value is a string that specifies the new value.\n" -"\n" -"This method can also set additional value and type information for the\n" -"specified key. The key identified by the key parameter must have been\n" -"opened with KEY_SET_VALUE access.\n" -"\n" -"To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n" -"\n" -"Value lengths are limited by available memory. Long values (more than\n" -"2048 bytes) should be stored as files with the filenames stored in \n" -"the configuration registry. This helps the registry perform efficiently."); - -PyDoc_STRVAR(DisableReflectionKey_doc, -"Disables registry reflection for 32-bit processes running on a 64-bit\n" -"Operating System. Will generally raise NotImplemented if executed on\n" -"a 32-bit Operating System.\n" -"\n" -"If the key is not on the reflection list, the function succeeds but has no effect.\n" -"Disabling reflection for a key does not affect reflection of any subkeys."); - -PyDoc_STRVAR(EnableReflectionKey_doc, -"Restores registry reflection for the specified disabled key.\n" -"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n" -"Restoring reflection for a key does not affect reflection of any subkeys."); - -PyDoc_STRVAR(QueryReflectionKey_doc, -"QueryReflectionKey(hkey) -> bool\n" -"Determines the reflection state for the specified key.\n" -"Will generally raise NotImplemented if executed on a 32-bit Operating System.\n"); - /* PyHKEY docstrings */ PyDoc_STRVAR(PyHKEY_doc, "PyHKEY Object - A Python object, representing a win32 registry key.\n" @@ -389,25 +115,6 @@ "rich comparison - Handle objects are compared using the handle value."); -PyDoc_STRVAR(PyHKEY_Close_doc, -"key.Close()\n" -"Closes the underlying Windows handle.\n" -"\n" -"If the handle is already closed, no error is raised."); - -PyDoc_STRVAR(PyHKEY_Detach_doc, -"key.Detach() -> int\n" -"Detaches the Windows handle from the handle object.\n" -"\n" -"The result is the value of the handle before it is detached. If the\n" -"handle is already detached, this will return zero.\n" -"\n" -"After calling this function, the handle is effectively invalidated,\n" -"but the handle is not closed. You would call this function when you\n" -"need the underlying win32 handle to exist beyond the lifetime of the\n" -"handle object."); - - /************************************************************************ The PyHKEY object definition @@ -516,16 +223,187 @@ PyHKEY_unaryFailureFunc, /* nb_float */ }; -static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args); -static PyObject *PyHKEY_DetachMethod(PyObject *self, PyObject *args); -static PyObject *PyHKEY_Enter(PyObject *self); -static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args); + +/************************************************************************ + + The PyHKEY object methods + +************************************************************************/ +/*[clinic input] +winreg.HKEYType.Close + +Closes the underlying Windows handle. + +If the handle is already closed, no error is raised. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_HKEYType_Close__doc__, +"Close()\n" +"Closes the underlying Windows handle.\n" +"\n" +"If the handle is already closed, no error is raised."); + +#define WINREG_HKEYTYPE_CLOSE_METHODDEF \ + {"Close", (PyCFunction)winreg_HKEYType_Close, METH_NOARGS, winreg_HKEYType_Close__doc__}, + +static PyObject * +winreg_HKEYType_Close_impl(PyObject *self); + +static PyObject * +winreg_HKEYType_Close(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = winreg_HKEYType_Close_impl(self); + + return return_value; +} + +static PyObject * +winreg_HKEYType_Close_impl(PyObject *self) +/*[clinic end generated code: checksum=f757a8929b73804b388ad5f97bf33d79b8976c58]*/ +{ + if (!PyHKEY_Close(self)) + return NULL; + Py_RETURN_NONE; +} + +/*[clinic input] +winreg.HKEYType.Detach + + self: self(type="PyHKEYObject *") + +Detaches the Windows handle from the handle object. + +The result is the value of the handle before it is detached. If the +handle is already detached, this will return zero. + +After calling this function, the handle is effectively invalidated, +but the handle is not closed. You would call this function when you +need the underlying win32 handle to exist beyond the lifetime of the +handle object. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_HKEYType_Detach__doc__, +"Detach()\n" +"Detaches the Windows handle from the handle object.\n" +"\n" +"The result is the value of the handle before it is detached. If the\n" +"handle is already detached, this will return zero.\n" +"\n" +"After calling this function, the handle is effectively invalidated,\n" +"but the handle is not closed. You would call this function when you\n" +"need the underlying win32 handle to exist beyond the lifetime of the\n" +"handle object."); + +#define WINREG_HKEYTYPE_DETACH_METHODDEF \ + {"Detach", (PyCFunction)winreg_HKEYType_Detach, METH_NOARGS, winreg_HKEYType_Detach__doc__}, + +static PyObject * +winreg_HKEYType_Detach_impl(PyHKEYObject *self); + +static PyObject * +winreg_HKEYType_Detach(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = winreg_HKEYType_Detach_impl((PyHKEYObject *)self); + + return return_value; +} + +static PyObject * +winreg_HKEYType_Detach_impl(PyHKEYObject *self) +/*[clinic end generated code: checksum=73ae7510b622906dd52a484c3eeb7046a7ca07f0]*/ +{ + void* ret; + ret = (void*)self->hkey; + self->hkey = 0; + return PyLong_FromVoidPtr(ret); +} + +/*[clinic input] +winreg.HKEYType.__enter__ +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_HKEYType___enter____doc__, +"__enter__()"); + +#define WINREG_HKEYTYPE___ENTER___METHODDEF \ + {"__enter__", (PyCFunction)winreg_HKEYType___enter__, METH_NOARGS, winreg_HKEYType___enter____doc__}, + +static PyObject * +winreg_HKEYType___enter___impl(PyObject *self); + +static PyObject * +winreg_HKEYType___enter__(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = winreg_HKEYType___enter___impl(self); + + return return_value; +} + +static PyObject * +winreg_HKEYType___enter___impl(PyObject *self) +/*[clinic end generated code: checksum=cacb13fcad333e970f778037a5fc5a650a6125b2]*/ +{ + Py_XINCREF(self); + return self; +} + + +/*[clinic input] +winreg.HKEYType.__exit__ + + exc_type: object + exc_value: object + traceback: object +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_HKEYType___exit____doc__, +"__exit__(exc_type, exc_value, traceback)"); + +#define WINREG_HKEYTYPE___EXIT___METHODDEF \ + {"__exit__", (PyCFunction)winreg_HKEYType___exit__, METH_VARARGS|METH_KEYWORDS, winreg_HKEYType___exit____doc__}, + +static PyObject * +winreg_HKEYType___exit___impl(PyObject *self, PyObject *exc_type, PyObject *exc_value, PyObject *traceback); + +static PyObject * +winreg_HKEYType___exit__(PyObject *self, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"exc_type", "exc_value", "traceback", NULL}; + PyObject *exc_type; + PyObject *exc_value; + PyObject *traceback; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "OOO:__exit__", _keywords, + &exc_type, &exc_value, &traceback)) + goto exit; + return_value = winreg_HKEYType___exit___impl(self, exc_type, exc_value, traceback); + +exit: + return return_value; +} + +static PyObject * +winreg_HKEYType___exit___impl(PyObject *self, PyObject *exc_type, PyObject *exc_value, PyObject *traceback) +/*[clinic end generated code: checksum=c959f30c3a02479cae5fb04cc50611491efeaffc]*/ +{ + if (!PyHKEY_Close(self)) + return NULL; + Py_RETURN_NONE; +} static struct PyMethodDef PyHKEY_methods[] = { - {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc}, - {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc}, - {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL}, - {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL}, + WINREG_HKEYTYPE_CLOSE_METHODDEF + WINREG_HKEYTYPE_DETACH_METHODDEF + WINREG_HKEYTYPE___ENTER___METHODDEF + WINREG_HKEYTYPE___EXIT___METHODDEF {NULL} }; @@ -570,50 +448,6 @@ }; /************************************************************************ - - The PyHKEY object methods - -************************************************************************/ -static PyObject * -PyHKEY_CloseMethod(PyObject *self, PyObject *args) -{ - if (!PyArg_ParseTuple(args, ":Close")) - return NULL; - if (!PyHKEY_Close(self)) - return NULL; - Py_INCREF(Py_None); - return Py_None; -} - -static PyObject * -PyHKEY_DetachMethod(PyObject *self, PyObject *args) -{ - void* ret; - PyHKEYObject *pThis = (PyHKEYObject *)self; - if (!PyArg_ParseTuple(args, ":Detach")) - return NULL; - ret = (void*)pThis->hkey; - pThis->hkey = 0; - return PyLong_FromVoidPtr(ret); -} - -static PyObject * -PyHKEY_Enter(PyObject *self) -{ - Py_XINCREF(self); - return self; -} - -static PyObject * -PyHKEY_Exit(PyObject *self, PyObject *args) -{ - if (!PyHKEY_Close(self)) - return NULL; - Py_RETURN_NONE; -} - - -/************************************************************************ The public PyHKEY API (well, not public yet :-) ************************************************************************/ PyObject * @@ -675,6 +509,14 @@ return TRUE; } +BOOL +clinic_HKEY_converter(PyObject *ob, void *p) +{ + if (!PyHKEY_AsHKEY(ob, (HKEY *)p, FALSE)) + return FALSE; + return TRUE; +} + PyObject * PyHKEY_FromHKEY(HKEY h) { @@ -980,32 +822,101 @@ /* The Python methods */ +/*[clinic input] +winreg.CloseKey + + hkey: object + A previously opened key. + / + +Closes a previously opened registry key. + +Note that if the key is not closed using this method, it will be +closed when the hkey object is destroyed by Python. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_CloseKey__doc__, +"CloseKey(hkey)\n" +"Closes a previously opened registry key.\n" +"\n" +" hkey\n" +" A previously opened key.\n" +"\n" +"Note that if the key is not closed using this method, it will be\n" +"closed when the hkey object is destroyed by Python."); + +#define WINREG_CLOSEKEY_METHODDEF \ + {"CloseKey", (PyCFunction)winreg_CloseKey, METH_O, winreg_CloseKey__doc__}, + static PyObject * -PyCloseKey(PyObject *self, PyObject *args) +winreg_CloseKey(PyModuleDef *module, PyObject *hkey) +/*[clinic end generated code: checksum=cfaef388f0014846b9e46b8da1a026f72980ec37]*/ { - PyObject *obKey; - if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey)) + if (!PyHKEY_Close(hkey)) return NULL; - if (!PyHKEY_Close(obKey)) - return NULL; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +winreg.ConnectRegistry + + computer_name: Py_UNICODE(nullable=True) + The name of the remote computer, of the form r"\\computername". If + None, the local computer is used. + key: HKEY + The predefined key to connect to. + / + +Establishes a connection to the registry on on another computer. + +The return value is the handle of the opened key. +If the function fails, an OSError exception is raised. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_ConnectRegistry__doc__, +"ConnectRegistry(computer_name, key)\n" +"Establishes a connection to the registry on on another computer.\n" +"\n" +" computer_name\n" +" The name of the remote computer, of the form r\"\\computername\". If\n" +" None, the local computer is used.\n" +" key\n" +" The predefined key to connect to.\n" +"\n" +"The return value is the handle of the opened key.\n" +"If the function fails, an OSError exception is raised."); + +#define WINREG_CONNECTREGISTRY_METHODDEF \ + {"ConnectRegistry", (PyCFunction)winreg_ConnectRegistry, METH_VARARGS, winreg_ConnectRegistry__doc__}, + static PyObject * -PyConnectRegistry(PyObject *self, PyObject *args) +winreg_ConnectRegistry_impl(PyModuleDef *module, Py_UNICODE *computer_name, HKEY key); + +static PyObject * +winreg_ConnectRegistry(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; - wchar_t *szCompName = NULL; + PyObject *return_value = NULL; + Py_UNICODE *computer_name; + HKEY key; + + if (!PyArg_ParseTuple(args, + "ZO&:ConnectRegistry", + &computer_name, clinic_HKEY_converter, &key)) + goto exit; + return_value = winreg_ConnectRegistry_impl(module, computer_name, key); + +exit: + return return_value; +} + +static PyObject * +winreg_ConnectRegistry_impl(PyModuleDef *module, Py_UNICODE *computer_name, HKEY key) +/*[clinic end generated code: checksum=7c6aafcb3e84579913df6f475a21e304bc3a5fa0]*/ +{ HKEY retKey; long rc; - if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; Py_BEGIN_ALLOW_THREADS - rc = RegConnectRegistryW(szCompName, hKey, &retKey); + rc = RegConnectRegistryW(computer_name, key, &retKey); Py_END_ALLOW_THREADS if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, @@ -1013,87 +924,313 @@ return PyHKEY_FromHKEY(retKey); } +/*[clinic input] +winreg.CreateKey + + key: HKEY + An already open key, or one of the predefined HKEY_* constants. + sub_key: Py_UNICODE(nullable=True) + The name of the key this method opens or creates. + / + +Creates or opens the specified key. + +If key is one of the predefined keys, sub_key may be None. In that case, +the handle returned is the same key handle passed in to the function. + +If the key already exists, this function opens the existing key. + +The return value is the handle of the opened key. +If the function fails, an OSError exception is raised. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_CreateKey__doc__, +"CreateKey(key, sub_key)\n" +"Creates or opens the specified key.\n" +"\n" +" key\n" +" An already open key, or one of the predefined HKEY_* constants.\n" +" sub_key\n" +" The name of the key this method opens or creates.\n" +"\n" +"If key is one of the predefined keys, sub_key may be None. In that case,\n" +"the handle returned is the same key handle passed in to the function.\n" +"\n" +"If the key already exists, this function opens the existing key.\n" +"\n" +"The return value is the handle of the opened key.\n" +"If the function fails, an OSError exception is raised."); + +#define WINREG_CREATEKEY_METHODDEF \ + {"CreateKey", (PyCFunction)winreg_CreateKey, METH_VARARGS, winreg_CreateKey__doc__}, + static PyObject * -PyCreateKey(PyObject *self, PyObject *args) +winreg_CreateKey_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key); + +static PyObject * +winreg_CreateKey(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; - wchar_t *subKey; + PyObject *return_value = NULL; + HKEY key; + Py_UNICODE *sub_key; + + if (!PyArg_ParseTuple(args, + "O&Z:CreateKey", + clinic_HKEY_converter, &key, &sub_key)) + goto exit; + return_value = winreg_CreateKey_impl(module, key, sub_key); + +exit: + return return_value; +} + +static PyObject * +winreg_CreateKey_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key) +/*[clinic end generated code: checksum=3796be4bf70c6dcaeb8e823382a2132a455248d1]*/ +{ HKEY retKey; long rc; - if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - rc = RegCreateKeyW(hKey, subKey, &retKey); + + rc = RegCreateKeyW(key, sub_key, &retKey); if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey"); return PyHKEY_FromHKEY(retKey); } +/*[clinic input] +winreg.CreateKeyEx + + key: HKEY + An already open key, or one of the predefined HKEY_* constants. + sub_key: Py_UNICODE(nullable=True) + The name of the key this method opens or creates. + reserved: int = 0 + A reserved integer, and must be zero. Default is zero. + access: REGSAM(c_default='KEY_WRITE') = winreg.KEY_WRITE + An integer that specifies an access mask that describes the + desired security access for the key. Default is KEY_WRITE. + +Creates or opens the specified key. + +If key is one of the predefined keys, sub_key may be None. In that case, +the handle returned is the same key handle passed in to the function. + +If the key already exists, this function opens the existing key + +The return value is the handle of the opened key. +If the function fails, an OSError exception is raised. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_CreateKeyEx__doc__, +"CreateKeyEx(key, sub_key, reserved=0, access=winreg.KEY_WRITE)\n" +"Creates or opens the specified key.\n" +"\n" +" key\n" +" An already open key, or one of the predefined HKEY_* constants.\n" +" sub_key\n" +" The name of the key this method opens or creates.\n" +" reserved\n" +" A reserved integer, and must be zero. Default is zero.\n" +" access\n" +" An integer that specifies an access mask that describes the\n" +" desired security access for the key. Default is KEY_WRITE.\n" +"\n" +"If key is one of the predefined keys, sub_key may be None. In that case,\n" +"the handle returned is the same key handle passed in to the function.\n" +"\n" +"If the key already exists, this function opens the existing key\n" +"\n" +"The return value is the handle of the opened key.\n" +"If the function fails, an OSError exception is raised."); + +#define WINREG_CREATEKEYEX_METHODDEF \ + {"CreateKeyEx", (PyCFunction)winreg_CreateKeyEx, METH_VARARGS|METH_KEYWORDS, winreg_CreateKeyEx__doc__}, + static PyObject * -PyCreateKeyEx(PyObject *self, PyObject *args, PyObject *kwargs) +winreg_CreateKeyEx_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, int reserved, REGSAM access); + +static PyObject * +winreg_CreateKeyEx(PyModuleDef *module, PyObject *args, PyObject *kwargs) { - HKEY hKey; - PyObject *key; - wchar_t *sub_key; - HKEY retKey; + PyObject *return_value = NULL; + static char *_keywords[] = {"key", "sub_key", "reserved", "access", NULL}; + HKEY key; + Py_UNICODE *sub_key; int reserved = 0; REGSAM access = KEY_WRITE; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O&Z|ii:CreateKeyEx", _keywords, + clinic_HKEY_converter, &key, &sub_key, &reserved, &access)) + goto exit; + return_value = winreg_CreateKeyEx_impl(module, key, sub_key, reserved, access); + +exit: + return return_value; +} + +static PyObject * +winreg_CreateKeyEx_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, int reserved, REGSAM access) +/*[clinic end generated code: checksum=0d9fb23e38ac50c3d34b03cba508618d0a954ed9]*/ +{ + HKEY retKey; long rc; - char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:CreateKeyEx", kwlist, - &key, &sub_key, &reserved, &access)) - return NULL; - if (!PyHKEY_AsHKEY(key, &hKey, FALSE)) - return NULL; - - rc = RegCreateKeyExW(hKey, sub_key, reserved, NULL, (DWORD)NULL, + rc = RegCreateKeyExW(key, sub_key, reserved, NULL, (DWORD)NULL, access, NULL, &retKey, NULL); if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx"); return PyHKEY_FromHKEY(retKey); } +/*[clinic input] +winreg.DeleteKey + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + sub_key: Py_UNICODE + A string that must be the name of a subkey of the key identified by + the key parameter. This value must not be None, and the key may not + have subkeys. + / + +Deletes the specified key. + +This method can not delete keys with subkeys. + +If the function succeeds, the entire key, including all of its values, +is removed. If the function fails, an OSError exception is raised. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_DeleteKey__doc__, +"DeleteKey(key, sub_key)\n" +"Deletes the specified key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" sub_key\n" +" A string that must be the name of a subkey of the key identified by\n" +" the key parameter. This value must not be None, and the key may not\n" +" have subkeys.\n" +"\n" +"This method can not delete keys with subkeys.\n" +"\n" +"If the function succeeds, the entire key, including all of its values,\n" +"is removed. If the function fails, an OSError exception is raised."); + +#define WINREG_DELETEKEY_METHODDEF \ + {"DeleteKey", (PyCFunction)winreg_DeleteKey, METH_VARARGS, winreg_DeleteKey__doc__}, + static PyObject * -PyDeleteKey(PyObject *self, PyObject *args) +winreg_DeleteKey_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key); + +static PyObject * +winreg_DeleteKey(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; - wchar_t *subKey; + PyObject *return_value = NULL; + HKEY key; + Py_UNICODE *sub_key; + + if (!PyArg_ParseTuple(args, + "O&u:DeleteKey", + clinic_HKEY_converter, &key, &sub_key)) + goto exit; + return_value = winreg_DeleteKey_impl(module, key, sub_key); + +exit: + return return_value; +} + +static PyObject * +winreg_DeleteKey_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key) +/*[clinic end generated code: checksum=34f36205bb8961c79ff4d0c755b62c2c7affaf62]*/ +{ long rc; - if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - rc = RegDeleteKeyW(hKey, subKey ); + rc = RegDeleteKeyW(key, sub_key ); if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +winreg.DeleteKeyEx + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + sub_key: Py_UNICODE + A string that must be the name of a subkey of the key identified by + the key parameter. This value must not be None, and the key may not + have subkeys. + access: REGSAM(c_default='KEY_WOW64_64KEY') = winreg.KEY_WOW64_64KEY + An integer that specifies an access mask that describes the + desired security access for the key. Default is KEY_WOW64_64KEY. + reserved: int = 0 + A reserved integer, and must be zero. Default is zero. + +Deletes the specified key (64-bit OS only). + +This method can not delete keys with subkeys. + +If the function succeeds, the entire key, including all of its values, +is removed. If the function fails, an OSError exception is raised. +On unsupported Windows versions, NotImplementedError is raised. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_DeleteKeyEx__doc__, +"DeleteKeyEx(key, sub_key, access=winreg.KEY_WOW64_64KEY, reserved=0)\n" +"Deletes the specified key (64-bit OS only).\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" sub_key\n" +" A string that must be the name of a subkey of the key identified by\n" +" the key parameter. This value must not be None, and the key may not\n" +" have subkeys.\n" +" access\n" +" An integer that specifies an access mask that describes the\n" +" desired security access for the key. Default is KEY_WOW64_64KEY.\n" +" reserved\n" +" A reserved integer, and must be zero. Default is zero.\n" +"\n" +"This method can not delete keys with subkeys.\n" +"\n" +"If the function succeeds, the entire key, including all of its values,\n" +"is removed. If the function fails, an OSError exception is raised.\n" +"On unsupported Windows versions, NotImplementedError is raised."); + +#define WINREG_DELETEKEYEX_METHODDEF \ + {"DeleteKeyEx", (PyCFunction)winreg_DeleteKeyEx, METH_VARARGS|METH_KEYWORDS, winreg_DeleteKeyEx__doc__}, + static PyObject * -PyDeleteKeyEx(PyObject *self, PyObject *args, PyObject *kwargs) +winreg_DeleteKeyEx_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, REGSAM access, int reserved); + +static PyObject * +winreg_DeleteKeyEx(PyModuleDef *module, PyObject *args, PyObject *kwargs) { - HKEY hKey; - PyObject *key; + PyObject *return_value = NULL; + static char *_keywords[] = {"key", "sub_key", "access", "reserved", NULL}; + HKEY key; + Py_UNICODE *sub_key; + REGSAM access = KEY_WOW64_64KEY; + int reserved = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O&u|ii:DeleteKeyEx", _keywords, + clinic_HKEY_converter, &key, &sub_key, &access, &reserved)) + goto exit; + return_value = winreg_DeleteKeyEx_impl(module, key, sub_key, access, reserved); + +exit: + return return_value; +} + +static PyObject * +winreg_DeleteKeyEx_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, REGSAM access, int reserved) +/*[clinic end generated code: checksum=2058c9c738a0f3fd0a1f2dbec69e9e8053c51268]*/ +{ HMODULE hMod; typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int); RDKEFunc pfn = NULL; - wchar_t *sub_key; long rc; - int reserved = 0; - REGSAM access = KEY_WOW64_64KEY; - - char *kwlist[] = {"key", "sub_key", "access", "reserved", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ou|ii:DeleteKeyEx", kwlist, - &key, &sub_key, &access, &reserved)) - return NULL; - if (!PyHKEY_AsHKEY(key, &hKey, FALSE)) - return NULL; /* Only available on 64bit platforms, so we must load it dynamically. */ @@ -1107,42 +1244,128 @@ return NULL; } Py_BEGIN_ALLOW_THREADS - rc = (*pfn)(hKey, sub_key, access, reserved); + rc = (*pfn)(key, sub_key, access, reserved); Py_END_ALLOW_THREADS if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +winreg.DeleteValue + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + value: Py_UNICODE(nullable=True) + A string that identifies the value to remove. + / + +Removes a named value from a registry key. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_DeleteValue__doc__, +"DeleteValue(key, value)\n" +"Removes a named value from a registry key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" value\n" +" A string that identifies the value to remove."); + +#define WINREG_DELETEVALUE_METHODDEF \ + {"DeleteValue", (PyCFunction)winreg_DeleteValue, METH_VARARGS, winreg_DeleteValue__doc__}, + static PyObject * -PyDeleteValue(PyObject *self, PyObject *args) +winreg_DeleteValue_impl(PyModuleDef *module, HKEY key, Py_UNICODE *value); + +static PyObject * +winreg_DeleteValue(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; - wchar_t *subKey; + PyObject *return_value = NULL; + HKEY key; + Py_UNICODE *value; + + if (!PyArg_ParseTuple(args, + "O&Z:DeleteValue", + clinic_HKEY_converter, &key, &value)) + goto exit; + return_value = winreg_DeleteValue_impl(module, key, value); + +exit: + return return_value; +} + +static PyObject * +winreg_DeleteValue_impl(PyModuleDef *module, HKEY key, Py_UNICODE *value) +/*[clinic end generated code: checksum=9e41c44021cfa7da2ed3d1343004c9a6bc73240d]*/ +{ long rc; - if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; Py_BEGIN_ALLOW_THREADS - rc = RegDeleteValueW(hKey, subKey); + rc = RegDeleteValueW(key, value); Py_END_ALLOW_THREADS if (rc !=ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteValue"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +winreg.EnumKey + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + index: int + An integer that identifies the index of the key to retrieve. + / + +Enumerates subkeys of an open registry key. + +The function retrieves the name of one subkey each time it is called. +It is typically called repeatedly until an OSError exception is +raised, indicating no more values are available. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_EnumKey__doc__, +"EnumKey(key, index)\n" +"Enumerates subkeys of an open registry key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" index\n" +" An integer that identifies the index of the key to retrieve.\n" +"\n" +"The function retrieves the name of one subkey each time it is called.\n" +"It is typically called repeatedly until an OSError exception is\n" +"raised, indicating no more values are available."); + +#define WINREG_ENUMKEY_METHODDEF \ + {"EnumKey", (PyCFunction)winreg_EnumKey, METH_VARARGS, winreg_EnumKey__doc__}, + static PyObject * -PyEnumKey(PyObject *self, PyObject *args) +winreg_EnumKey_impl(PyModuleDef *module, HKEY key, int index); + +static PyObject * +winreg_EnumKey(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; + PyObject *return_value = NULL; + HKEY key; int index; + + if (!PyArg_ParseTuple(args, + "O&i:EnumKey", + clinic_HKEY_converter, &key, &index)) + goto exit; + return_value = winreg_EnumKey_impl(module, key, index); + +exit: + return return_value; +} + +static PyObject * +winreg_EnumKey_impl(PyModuleDef *module, HKEY key, int index) +/*[clinic end generated code: checksum=2aab01d9bd466dee06bd881bffb7088444954e13]*/ +{ long rc; PyObject *retStr; @@ -1154,14 +1377,9 @@ * retrieve such a key name. */ wchar_t tmpbuf[257]; DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */ - - if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - + Py_BEGIN_ALLOW_THREADS - rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL); + rc = RegEnumKeyExW(key, index, tmpbuf, &len, NULL, NULL, NULL, NULL); Py_END_ALLOW_THREADS if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx"); @@ -1170,12 +1388,80 @@ return retStr; /* can be NULL */ } +/*[clinic input] +winreg.EnumValue + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + index: int + An integer that identifies the index of the value to retrieve. + / + +Enumerates values of an open registry key. + +The function retrieves the name of one subkey each time it is called. +It is typically called repeatedly, until an OSError exception +is raised, indicating no more values. + +The result is a tuple of 3 items: + value_name + A string that identifies the value. + value_data + An object that holds the value data, and whose type depends + on the underlying registry type. + data_type + An integer that identifies the type of the value data. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_EnumValue__doc__, +"EnumValue(key, index)\n" +"Enumerates values of an open registry key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" index\n" +" An integer that identifies the index of the value to retrieve.\n" +"\n" +"The function retrieves the name of one subkey each time it is called.\n" +"It is typically called repeatedly, until an OSError exception\n" +"is raised, indicating no more values.\n" +"\n" +"The result is a tuple of 3 items:\n" +" value_name\n" +" A string that identifies the value.\n" +" value_data\n" +" An object that holds the value data, and whose type depends\n" +" on the underlying registry type.\n" +" data_type\n" +" An integer that identifies the type of the value data."); + +#define WINREG_ENUMVALUE_METHODDEF \ + {"EnumValue", (PyCFunction)winreg_EnumValue, METH_VARARGS, winreg_EnumValue__doc__}, + static PyObject * -PyEnumValue(PyObject *self, PyObject *args) +winreg_EnumValue_impl(PyModuleDef *module, HKEY key, int index); + +static PyObject * +winreg_EnumValue(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; + PyObject *return_value = NULL; + HKEY key; int index; + + if (!PyArg_ParseTuple(args, + "O&i:EnumValue", + clinic_HKEY_converter, &key, &index)) + goto exit; + return_value = winreg_EnumValue_impl(module, key, index); + +exit: + return return_value; +} + +static PyObject * +winreg_EnumValue_impl(PyModuleDef *module, HKEY key, int index) +/*[clinic end generated code: checksum=0a95fa55708fe51b22d1e8e756814a804983b74b]*/ +{ long rc; wchar_t *retValueBuf; BYTE *tmpBuf; @@ -1186,12 +1472,7 @@ PyObject *obData; PyObject *retVal; - if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - - if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL, + if ((rc = RegQueryInfoKeyW(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &retValueSize, &retDataSize, NULL, NULL)) != ERROR_SUCCESS) @@ -1212,7 +1493,7 @@ while (1) { Py_BEGIN_ALLOW_THREADS - rc = RegEnumValueW(hKey, + rc = RegEnumValueW(key, index, retValueBuf, &retValueSize, @@ -1255,19 +1536,51 @@ return retVal; } +/*[clinic input] +winreg.ExpandEnvironmentStrings + + string: Py_UNICODE + / + +Expand environment vars. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_ExpandEnvironmentStrings__doc__, +"ExpandEnvironmentStrings(string)\n" +"Expand environment vars."); + +#define WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF \ + {"ExpandEnvironmentStrings", (PyCFunction)winreg_ExpandEnvironmentStrings, METH_VARARGS, winreg_ExpandEnvironmentStrings__doc__}, + static PyObject * -PyExpandEnvironmentStrings(PyObject *self, PyObject *args) +winreg_ExpandEnvironmentStrings_impl(PyModuleDef *module, Py_UNICODE *string); + +static PyObject * +winreg_ExpandEnvironmentStrings(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + Py_UNICODE *string; + + if (!PyArg_ParseTuple(args, + "u:ExpandEnvironmentStrings", + &string)) + goto exit; + return_value = winreg_ExpandEnvironmentStrings_impl(module, string); + +exit: + return return_value; +} + +static PyObject * +winreg_ExpandEnvironmentStrings_impl(PyModuleDef *module, Py_UNICODE *string) +/*[clinic end generated code: checksum=f6ba8883b207d14ec3905eb008b8a7da7d0e39a7]*/ { wchar_t *retValue = NULL; - wchar_t *src; DWORD retValueSize; DWORD rc; PyObject *o; - if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src)) - return NULL; - - retValueSize = ExpandEnvironmentStringsW(src, retValue, 0); + retValueSize = ExpandEnvironmentStringsW(string, retValue, 0); if (retValueSize == 0) { return PyErr_SetFromWindowsErrWithFunction(retValueSize, "ExpandEnvironmentStrings"); @@ -1277,7 +1590,7 @@ return PyErr_NoMemory(); } - rc = ExpandEnvironmentStringsW(src, retValue, retValueSize); + rc = ExpandEnvironmentStringsW(string, retValue, retValueSize); if (rc == 0) { PyMem_Free(retValue); return PyErr_SetFromWindowsErrWithFunction(retValueSize, @@ -1288,90 +1601,371 @@ return o; } +/*[clinic input] +winreg.FlushKey + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + / + +Writes all the attributes of a key to the registry. + +It is not necessary to call FlushKey to change a key. Registry changes +are flushed to disk by the registry using its lazy flusher. Registry +changes are also flushed to disk at system shutdown. Unlike +CloseKey(), the FlushKey() method returns only when all the data has +been written to the registry. + +An application should only call FlushKey() if it requires absolute +certainty that registry changes are on disk. If you don't know whether +a FlushKey() call is required, it probably isn't. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_FlushKey__doc__, +"FlushKey(key)\n" +"Writes all the attributes of a key to the registry.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +"\n" +"It is not necessary to call FlushKey to change a key. Registry changes\n" +"are flushed to disk by the registry using its lazy flusher. Registry\n" +"changes are also flushed to disk at system shutdown. Unlike\n" +"CloseKey(), the FlushKey() method returns only when all the data has\n" +"been written to the registry.\n" +"\n" +"An application should only call FlushKey() if it requires absolute\n" +"certainty that registry changes are on disk. If you don\'t know whether\n" +"a FlushKey() call is required, it probably isn\'t."); + +#define WINREG_FLUSHKEY_METHODDEF \ + {"FlushKey", (PyCFunction)winreg_FlushKey, METH_VARARGS, winreg_FlushKey__doc__}, + static PyObject * -PyFlushKey(PyObject *self, PyObject *args) +winreg_FlushKey_impl(PyModuleDef *module, HKEY key); + +static PyObject * +winreg_FlushKey(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; + PyObject *return_value = NULL; + HKEY key; + + if (!PyArg_ParseTuple(args, + "O&:FlushKey", + clinic_HKEY_converter, &key)) + goto exit; + return_value = winreg_FlushKey_impl(module, key); + +exit: + return return_value; +} + +static PyObject * +winreg_FlushKey_impl(PyModuleDef *module, HKEY key) +/*[clinic end generated code: checksum=e6345ceb923776a4dd01b7857df8f5337141d8c7]*/ +{ long rc; - if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; Py_BEGIN_ALLOW_THREADS - rc = RegFlushKey(hKey); + rc = RegFlushKey(key); Py_END_ALLOW_THREADS if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } + + +/*[clinic input] +winreg.LoadKey + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + sub_key: Py_UNICODE + A string that identifies the sub-key to load. + file_name: Py_UNICODE + The name of the file to load registry data from. This file must + have been created with the SaveKey() function. Under the file + allocation table (FAT) file system, the filename may not have an + extension. + / + +Insert data into the registry from a file. + +Creates a subkey under the specified key and stores registration +information from a specified file into that subkey. + +A call to LoadKey() fails if the calling process does not have the +SE_RESTORE_PRIVILEGE privilege. + +If key is a handle returned by ConnectRegistry(), then the path +specified in fileName is relative to the remote computer. + +The MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE +tree. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_LoadKey__doc__, +"LoadKey(key, sub_key, file_name)\n" +"Insert data into the registry from a file.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" sub_key\n" +" A string that identifies the sub-key to load.\n" +" file_name\n" +" The name of the file to load registry data from. This file must\n" +" have been created with the SaveKey() function. Under the file\n" +" allocation table (FAT) file system, the filename may not have an\n" +" extension.\n" +"\n" +"Creates a subkey under the specified key and stores registration\n" +"information from a specified file into that subkey.\n" +"\n" +"A call to LoadKey() fails if the calling process does not have the\n" +"SE_RESTORE_PRIVILEGE privilege.\n" +"\n" +"If key is a handle returned by ConnectRegistry(), then the path\n" +"specified in fileName is relative to the remote computer.\n" +"\n" +"The MSDN docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE\n" +"tree."); + +#define WINREG_LOADKEY_METHODDEF \ + {"LoadKey", (PyCFunction)winreg_LoadKey, METH_VARARGS, winreg_LoadKey__doc__}, + static PyObject * -PyLoadKey(PyObject *self, PyObject *args) +winreg_LoadKey_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, Py_UNICODE *file_name); + +static PyObject * +winreg_LoadKey(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; - wchar_t *subKey; - wchar_t *fileName; - + PyObject *return_value = NULL; + HKEY key; + Py_UNICODE *sub_key; + Py_UNICODE *file_name; + + if (!PyArg_ParseTuple(args, + "O&uu:LoadKey", + clinic_HKEY_converter, &key, &sub_key, &file_name)) + goto exit; + return_value = winreg_LoadKey_impl(module, key, sub_key, file_name); + +exit: + return return_value; +} + +static PyObject * +winreg_LoadKey_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, Py_UNICODE *file_name) +/*[clinic end generated code: checksum=1b128c84e147b0e9e065ceabe62d1262a3ac4b35]*/ +{ long rc; - if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; + Py_BEGIN_ALLOW_THREADS - rc = RegLoadKeyW(hKey, subKey, fileName ); + rc = RegLoadKeyW(key, sub_key, file_name ); Py_END_ALLOW_THREADS if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +winreg.OpenKey + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + sub_key: Py_UNICODE(nullable=True) + A string that identifies the sub_key to open. + reserved: int = 0 + A reserved integer that must be zero. Default is zero. + access: REGSAM(c_default='KEY_READ') = winreg.KEY_READ + An integer that specifies an access mask that describes the desired + security access for the key. Default is KEY_READ. + +Opens the specified key. + +The result is a new handle to the specified key. +If the function fails, an OSError exception is raised. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_OpenKey__doc__, +"OpenKey(key, sub_key, reserved=0, access=winreg.KEY_READ)\n" +"Opens the specified key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" sub_key\n" +" A string that identifies the sub_key to open.\n" +" reserved\n" +" A reserved integer that must be zero. Default is zero.\n" +" access\n" +" An integer that specifies an access mask that describes the desired\n" +" security access for the key. Default is KEY_READ.\n" +"\n" +"The result is a new handle to the specified key.\n" +"If the function fails, an OSError exception is raised."); + +#define WINREG_OPENKEY_METHODDEF \ + {"OpenKey", (PyCFunction)winreg_OpenKey, METH_VARARGS|METH_KEYWORDS, winreg_OpenKey__doc__}, + static PyObject * -PyOpenKey(PyObject *self, PyObject *args, PyObject *kwargs) +winreg_OpenKey_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, int reserved, REGSAM access); + +static PyObject * +winreg_OpenKey(PyModuleDef *module, PyObject *args, PyObject *kwargs) { - HKEY hKey; - PyObject *key; - wchar_t *sub_key; + PyObject *return_value = NULL; + static char *_keywords[] = {"key", "sub_key", "reserved", "access", NULL}; + HKEY key; + Py_UNICODE *sub_key; int reserved = 0; + REGSAM access = KEY_READ; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O&Z|ii:OpenKey", _keywords, + clinic_HKEY_converter, &key, &sub_key, &reserved, &access)) + goto exit; + return_value = winreg_OpenKey_impl(module, key, sub_key, reserved, access); + +exit: + return return_value; +} + +static PyObject * +winreg_OpenKey_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, int reserved, REGSAM access) +/*[clinic end generated code: checksum=edc7555f37ae93744020e1bb132fb888b7f6f681]*/ +{ HKEY retKey; long rc; - REGSAM access = KEY_READ; - - char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL}; - - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:OpenKey", kwlist, - &key, &sub_key, &reserved, &access)) - return NULL; - if (!PyHKEY_AsHKEY(key, &hKey, FALSE)) - return NULL; Py_BEGIN_ALLOW_THREADS - rc = RegOpenKeyExW(hKey, sub_key, reserved, access, &retKey); + rc = RegOpenKeyExW(key, sub_key, reserved, access, &retKey); Py_END_ALLOW_THREADS if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx"); return PyHKEY_FromHKEY(retKey); } +/*[clinic input] +winreg.OpenKeyEx = winreg.OpenKey + +Opens the specified key. + +The result is a new handle to the specified key. +If the function fails, an OSError exception is raised. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_OpenKeyEx__doc__, +"OpenKeyEx(key, sub_key, reserved=0, access=winreg.KEY_READ)\n" +"Opens the specified key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" sub_key\n" +" A string that identifies the sub_key to open.\n" +" reserved\n" +" A reserved integer that must be zero. Default is zero.\n" +" access\n" +" An integer that specifies an access mask that describes the desired\n" +" security access for the key. Default is KEY_READ.\n" +"\n" +"The result is a new handle to the specified key.\n" +"If the function fails, an OSError exception is raised."); + +#define WINREG_OPENKEYEX_METHODDEF \ + {"OpenKeyEx", (PyCFunction)winreg_OpenKeyEx, METH_VARARGS|METH_KEYWORDS, winreg_OpenKeyEx__doc__}, static PyObject * -PyQueryInfoKey(PyObject *self, PyObject *args) +winreg_OpenKeyEx_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, int reserved, REGSAM access); + +static PyObject * +winreg_OpenKeyEx(PyModuleDef *module, PyObject *args, PyObject *kwargs) { - HKEY hKey; - PyObject *obKey; + PyObject *return_value = NULL; + static char *_keywords[] = {"key", "sub_key", "reserved", "access", NULL}; + HKEY key; + Py_UNICODE *sub_key; + int reserved = 0; + REGSAM access = KEY_READ; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "O&Z|ii:OpenKeyEx", _keywords, + clinic_HKEY_converter, &key, &sub_key, &reserved, &access)) + goto exit; + return_value = winreg_OpenKeyEx_impl(module, key, sub_key, reserved, access); + +exit: + return return_value; +} + +static PyObject * +winreg_OpenKeyEx_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, int reserved, REGSAM access) +/*[clinic end generated code: checksum=670b22d89c310c7545c135e994ad02658fbaf5e7]*/ +{ + return winreg_OpenKey_impl(module, key, sub_key, reserved, access); +} + +/*[clinic input] +winreg.QueryInfoKey + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + / + +Returns information about a key. + +The result is a tuple of 3 items: +An integer that identifies the number of sub keys this key has. +An integer that identifies the number of values this key has. +An integer that identifies when the key was last modified (if available) +as 100's of nanoseconds since Jan 1, 1600. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_QueryInfoKey__doc__, +"QueryInfoKey(key)\n" +"Returns information about a key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +"\n" +"The result is a tuple of 3 items:\n" +"An integer that identifies the number of sub keys this key has.\n" +"An integer that identifies the number of values this key has.\n" +"An integer that identifies when the key was last modified (if available)\n" +"as 100\'s of nanoseconds since Jan 1, 1600."); + +#define WINREG_QUERYINFOKEY_METHODDEF \ + {"QueryInfoKey", (PyCFunction)winreg_QueryInfoKey, METH_VARARGS, winreg_QueryInfoKey__doc__}, + +static PyObject * +winreg_QueryInfoKey_impl(PyModuleDef *module, HKEY key); + +static PyObject * +winreg_QueryInfoKey(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + HKEY key; + + if (!PyArg_ParseTuple(args, + "O&:QueryInfoKey", + clinic_HKEY_converter, &key)) + goto exit; + return_value = winreg_QueryInfoKey_impl(module, key); + +exit: + return return_value; +} + +static PyObject * +winreg_QueryInfoKey_impl(PyModuleDef *module, HKEY key) +/*[clinic end generated code: checksum=6821c973363b79318ab9ba91485ff763afe989f0]*/ +{ long rc; DWORD nSubKeys, nValues; FILETIME ft; LARGE_INTEGER li; PyObject *l; PyObject *ret; - if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL, + + if ((rc = RegQueryInfoKey(key, NULL, NULL, 0, &nSubKeys, NULL, NULL, &nValues, NULL, NULL, NULL, &ft)) != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey"); @@ -1385,12 +1979,72 @@ return ret; } +/*[clinic input] +winreg.QueryValue + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + sub_key: Py_UNICODE(nullable=True) + A string that holds the name of the subkey with which the value + is associated. If this parameter is None or empty, the function + retrieves the value set by the SetValue() method for the key + identified by key. + / + +Retrieves the unnamed value for a key. + +Values in the registry have name, type, and data components. This method +retrieves the data for a key's first value that has a NULL name. +But since the underlying API call doesn't return the type, you'll +probably be happier using QueryValueEx; this function is just here for +completeness. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_QueryValue__doc__, +"QueryValue(key, sub_key)\n" +"Retrieves the unnamed value for a key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" sub_key\n" +" A string that holds the name of the subkey with which the value\n" +" is associated. If this parameter is None or empty, the function\n" +" retrieves the value set by the SetValue() method for the key\n" +" identified by key.\n" +"\n" +"Values in the registry have name, type, and data components. This method\n" +"retrieves the data for a key\'s first value that has a NULL name.\n" +"But since the underlying API call doesn\'t return the type, you\'ll\n" +"probably be happier using QueryValueEx; this function is just here for\n" +"completeness."); + +#define WINREG_QUERYVALUE_METHODDEF \ + {"QueryValue", (PyCFunction)winreg_QueryValue, METH_VARARGS, winreg_QueryValue__doc__}, + static PyObject * -PyQueryValue(PyObject *self, PyObject *args) +winreg_QueryValue_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key); + +static PyObject * +winreg_QueryValue(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; - wchar_t *subKey; + PyObject *return_value = NULL; + HKEY key; + Py_UNICODE *sub_key; + + if (!PyArg_ParseTuple(args, + "O&Z:QueryValue", + clinic_HKEY_converter, &key, &sub_key)) + goto exit; + return_value = winreg_QueryValue_impl(module, key, sub_key); + +exit: + return return_value; +} + +static PyObject * +winreg_QueryValue_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key) +/*[clinic end generated code: checksum=d2a44b479a42dec73c4dccc7741bcdfd75b68b2e]*/ +{ long rc; PyObject *retStr; wchar_t *retBuf; @@ -1398,13 +2052,7 @@ DWORD retSize = 0; wchar_t *tmp; - if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey)) - return NULL; - - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - - rc = RegQueryValueW(hKey, subKey, NULL, &retSize); + rc = RegQueryValueW(key, sub_key, NULL, &retSize); if (rc == ERROR_MORE_DATA) retSize = 256; else if (rc != ERROR_SUCCESS) @@ -1418,7 +2066,7 @@ while (1) { retSize = bufSize; - rc = RegQueryValueW(hKey, subKey, retBuf, &retSize); + rc = RegQueryValueW(key, sub_key, retBuf, &retSize); if (rc != ERROR_MORE_DATA) break; @@ -1442,13 +2090,65 @@ return retStr; } + +/*[clinic input] +winreg.QueryValueEx + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + value_name: Py_UNICODE(nullable=True) + A string indicating the value to query. + / + +Retrieves the type and value of a specified sub-key. + +Behaves mostly like QueryValue(), but also returns the type of the +specified value name associated with the given open registry key. + +The return value is a tuple of the value and the type_id. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_QueryValueEx__doc__, +"QueryValueEx(key, value_name)\n" +"Retrieves the type and value of a specified sub-key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" value_name\n" +" A string indicating the value to query.\n" +"\n" +"Behaves mostly like QueryValue(), but also returns the type of the\n" +"specified value name associated with the given open registry key.\n" +"\n" +"The return value is a tuple of the value and the type_id."); + +#define WINREG_QUERYVALUEEX_METHODDEF \ + {"QueryValueEx", (PyCFunction)winreg_QueryValueEx, METH_VARARGS, winreg_QueryValueEx__doc__}, + static PyObject * -PyQueryValueEx(PyObject *self, PyObject *args) +winreg_QueryValueEx_impl(PyModuleDef *module, HKEY key, Py_UNICODE *value_name); + +static PyObject * +winreg_QueryValueEx(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; - wchar_t *valueName; - + PyObject *return_value = NULL; + HKEY key; + Py_UNICODE *value_name; + + if (!PyArg_ParseTuple(args, + "O&Z:QueryValueEx", + clinic_HKEY_converter, &key, &value_name)) + goto exit; + return_value = winreg_QueryValueEx_impl(module, key, value_name); + +exit: + return return_value; +} + +static PyObject * +winreg_QueryValueEx_impl(PyModuleDef *module, HKEY key, Py_UNICODE *value_name) +/*[clinic end generated code: checksum=705112a81bef8f4b4bc8009f2afc6ff0136e8198]*/ +{ long rc; BYTE *retBuf, *tmp; DWORD bufSize = 0, retSize; @@ -1456,13 +2156,7 @@ PyObject *obData; PyObject *result; - if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName)) - return NULL; - - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - - rc = RegQueryValueExW(hKey, valueName, NULL, NULL, NULL, &bufSize); + rc = RegQueryValueExW(key, value_name, NULL, NULL, NULL, &bufSize); if (rc == ERROR_MORE_DATA) bufSize = 256; else if (rc != ERROR_SUCCESS) @@ -1474,7 +2168,7 @@ while (1) { retSize = bufSize; - rc = RegQueryValueExW(hKey, valueName, NULL, &typ, + rc = RegQueryValueExW(key, value_name, NULL, &typ, (BYTE *)retBuf, &retSize); if (rc != ERROR_MORE_DATA) break; @@ -1502,91 +2196,303 @@ return result; } +/*[clinic input] +winreg.SaveKey + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + file_name: Py_UNICODE + The name of the file to save registry data to. This file cannot + already exist. If this filename includes an extension, it cannot be + used on file allocation table (FAT) file systems by the LoadKey(), + ReplaceKey() or RestoreKey() methods. + / + +Saves the specified key, and all its subkeys to the specified file. + +If key represents a key on a remote computer, the path described by +file_name is relative to the remote computer. + +The caller of this method must possess the SeBackupPrivilege +security privilege. This function passes NULL for security_attributes +to the API. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_SaveKey__doc__, +"SaveKey(key, file_name)\n" +"Saves the specified key, and all its subkeys to the specified file.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" file_name\n" +" The name of the file to save registry data to. This file cannot\n" +" already exist. If this filename includes an extension, it cannot be\n" +" used on file allocation table (FAT) file systems by the LoadKey(),\n" +" ReplaceKey() or RestoreKey() methods.\n" +"\n" +"If key represents a key on a remote computer, the path described by\n" +"file_name is relative to the remote computer.\n" +"\n" +"The caller of this method must possess the SeBackupPrivilege\n" +"security privilege. This function passes NULL for security_attributes\n" +"to the API."); + +#define WINREG_SAVEKEY_METHODDEF \ + {"SaveKey", (PyCFunction)winreg_SaveKey, METH_VARARGS, winreg_SaveKey__doc__}, static PyObject * -PySaveKey(PyObject *self, PyObject *args) +winreg_SaveKey_impl(PyModuleDef *module, HKEY key, Py_UNICODE *file_name); + +static PyObject * +winreg_SaveKey(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; - wchar_t *fileName; + PyObject *return_value = NULL; + HKEY key; + Py_UNICODE *file_name; + + if (!PyArg_ParseTuple(args, + "O&u:SaveKey", + clinic_HKEY_converter, &key, &file_name)) + goto exit; + return_value = winreg_SaveKey_impl(module, key, file_name); + +exit: + return return_value; +} + +static PyObject * +winreg_SaveKey_impl(PyModuleDef *module, HKEY key, Py_UNICODE *file_name) +/*[clinic end generated code: checksum=754688ce199b0dd7c46a2a9644d9338ab9dc9729]*/ +{ LPSECURITY_ATTRIBUTES pSA = NULL; long rc; - if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; /* One day we may get security into the core? if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE)) return NULL; */ Py_BEGIN_ALLOW_THREADS - rc = RegSaveKeyW(hKey, fileName, pSA ); + rc = RegSaveKeyW(key, file_name, pSA ); Py_END_ALLOW_THREADS if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +winreg.SetValue + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + sub_key: Py_UNICODE(nullable=True) + A string that names the subkey with which the value is associated. + type: DWORD + An integer that specifies the type of the data. Currently this must + be REG_SZ, meaning only strings are supported. + value: Py_UNICODE(length=True) + A string that specifies the new value. + / + +Associates a value with a specified key. + +If the key specified by the sub_key parameter does not exist, the +SetValue function creates it. + +Value lengths are limited by available memory. Long values (more than +2048 bytes) should be stored as files with the filenames stored in +the configuration registry to help the registry perform efficiently. + +The key identified by the key parameter must have been opened with +KEY_SET_VALUE access. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_SetValue__doc__, +"SetValue(key, sub_key, type, value)\n" +"Associates a value with a specified key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" sub_key\n" +" A string that names the subkey with which the value is associated.\n" +" type\n" +" An integer that specifies the type of the data. Currently this must\n" +" be REG_SZ, meaning only strings are supported.\n" +" value\n" +" A string that specifies the new value.\n" +"\n" +"If the key specified by the sub_key parameter does not exist, the\n" +"SetValue function creates it.\n" +"\n" +"Value lengths are limited by available memory. Long values (more than\n" +"2048 bytes) should be stored as files with the filenames stored in\n" +"the configuration registry to help the registry perform efficiently.\n" +"\n" +"The key identified by the key parameter must have been opened with\n" +"KEY_SET_VALUE access."); + +#define WINREG_SETVALUE_METHODDEF \ + {"SetValue", (PyCFunction)winreg_SetValue, METH_VARARGS, winreg_SetValue__doc__}, + static PyObject * -PySetValue(PyObject *self, PyObject *args) +winreg_SetValue_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, DWORD type_value, Py_UNICODE *value, Py_ssize_clean_t value_length); + +static PyObject * +winreg_SetValue(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; - wchar_t *subKey; - wchar_t *str; - DWORD typ; - DWORD len; + PyObject *return_value = NULL; + HKEY key; + Py_UNICODE *sub_key; + DWORD type_value; + Py_UNICODE *value; + Py_ssize_clean_t value_length; + + if (!PyArg_ParseTuple(args, + "O&Ziu#:SetValue", + clinic_HKEY_converter, &key, &sub_key, &type_value, &value, &value_length)) + goto exit; + return_value = winreg_SetValue_impl(module, key, sub_key, type_value, value, value_length); + +exit: + return return_value; +} + +static PyObject * +winreg_SetValue_impl(PyModuleDef *module, HKEY key, Py_UNICODE *sub_key, DWORD type_value, Py_UNICODE *value, Py_ssize_clean_t value_length) +/*[clinic end generated code: checksum=8560c50fd13dd83b036397595a2786058518e097]*/ +{ long rc; - if (!PyArg_ParseTuple(args, "OZiu#:SetValue", - &obKey, - &subKey, - &typ, - &str, - &len)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - if (typ != REG_SZ) { + + if (type_value != REG_SZ) { PyErr_SetString(PyExc_TypeError, "Type must be winreg.REG_SZ"); return NULL; } Py_BEGIN_ALLOW_THREADS - rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1); + rc = RegSetValueW(key, sub_key, REG_SZ, value, value_length+1); Py_END_ALLOW_THREADS if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +winreg.SetValueEx + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + value_name: Py_UNICODE(nullable=True) + A string containing the name of the value to set, or None. + reserved: object + Can be anything - zero is always passed to the API. + type: DWORD + An integer that specifies the type of the data, one of: + REG_BINARY -- Binary data in any form. + REG_DWORD -- A 32-bit number. + REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. + REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format. + REG_EXPAND_SZ -- A null-terminated string that contains unexpanded + references to environment variables (for example, + %PATH%). + REG_LINK -- A Unicode symbolic link. + REG_MULTI_SZ -- An sequence of null-terminated strings, terminated + by two null characters. Note that Python handles + this termination automatically. + REG_NONE -- No defined value type. + REG_RESOURCE_LIST -- A device-driver resource list. + REG_SZ -- A null-terminated string. + value: object + A string that specifies the new value. + / + +Stores data in the value field of an open registry key. + +This method can also set additional value and type information for the +specified key. The key identified by the key parameter must have been +opened with KEY_SET_VALUE access. + +To open the key, use the CreateKeyEx() or OpenKeyEx() methods. + +Value lengths are limited by available memory. Long values (more than +2048 bytes) should be stored as files with the filenames stored in +the configuration registry to help the registry perform efficiently. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_SetValueEx__doc__, +"SetValueEx(key, value_name, reserved, type, value)\n" +"Stores data in the value field of an open registry key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +" value_name\n" +" A string containing the name of the value to set, or None.\n" +" reserved\n" +" Can be anything - zero is always passed to the API.\n" +" type\n" +" An integer that specifies the type of the data, one of:\n" +" REG_BINARY -- Binary data in any form.\n" +" REG_DWORD -- A 32-bit number.\n" +" REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n" +" REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n" +" REG_EXPAND_SZ -- A null-terminated string that contains unexpanded\n" +" references to environment variables (for example,\n" +" %PATH%).\n" +" REG_LINK -- A Unicode symbolic link.\n" +" REG_MULTI_SZ -- An sequence of null-terminated strings, terminated\n" +" by two null characters. Note that Python handles\n" +" this termination automatically.\n" +" REG_NONE -- No defined value type.\n" +" REG_RESOURCE_LIST -- A device-driver resource list.\n" +" REG_SZ -- A null-terminated string.\n" +" value\n" +" A string that specifies the new value.\n" +"\n" +"This method can also set additional value and type information for the\n" +"specified key. The key identified by the key parameter must have been\n" +"opened with KEY_SET_VALUE access.\n" +"\n" +"To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n" +"\n" +"Value lengths are limited by available memory. Long values (more than\n" +"2048 bytes) should be stored as files with the filenames stored in\n" +"the configuration registry to help the registry perform efficiently."); + +#define WINREG_SETVALUEEX_METHODDEF \ + {"SetValueEx", (PyCFunction)winreg_SetValueEx, METH_VARARGS, winreg_SetValueEx__doc__}, + static PyObject * -PySetValueEx(PyObject *self, PyObject *args) +winreg_SetValueEx_impl(PyModuleDef *module, HKEY key, Py_UNICODE *value_name, PyObject *reserved, DWORD type_value, PyObject *value); + +static PyObject * +winreg_SetValueEx(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; - wchar_t *valueName; - PyObject *obRes; + PyObject *return_value = NULL; + HKEY key; + Py_UNICODE *value_name; + PyObject *reserved; + DWORD type_value; PyObject *value; + + if (!PyArg_ParseTuple(args, + "O&ZOiO:SetValueEx", + clinic_HKEY_converter, &key, &value_name, &reserved, &type_value, &value)) + goto exit; + return_value = winreg_SetValueEx_impl(module, key, value_name, reserved, type_value, value); + +exit: + return return_value; +} + +static PyObject * +winreg_SetValueEx_impl(PyModuleDef *module, HKEY key, Py_UNICODE *value_name, PyObject *reserved, DWORD type_value, PyObject *value) +/*[clinic end generated code: checksum=c94e755cb8edfb679476fa667bad6770b942999b]*/ +{ BYTE *data; DWORD len; - DWORD typ; LONG rc; - if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx", - &obKey, - &valueName, - &obRes, - &typ, - &value)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - if (!Py2Reg(value, typ, &data, &len)) + if (!Py2Reg(value, type_value, &data, &len)) { if (!PyErr_Occurred()) PyErr_SetString(PyExc_ValueError, @@ -1594,31 +2500,75 @@ return NULL; } Py_BEGIN_ALLOW_THREADS - rc = RegSetValueExW(hKey, valueName, 0, typ, data, len); + rc = RegSetValueExW(key, value_name, 0, type_value, data, len); Py_END_ALLOW_THREADS PyMem_DEL(data); if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValueEx"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +winreg.DisableReflectionKey + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + / + +Disables registry reflection for 32bit processes running on a 64bit OS. + +Will generally raise NotImplemented if executed on a 32bit OS. + +If the key is not on the reflection list, the function succeeds but has +no effect. Disabling reflection for a key does not affect reflection +of any subkeys. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_DisableReflectionKey__doc__, +"DisableReflectionKey(key)\n" +"Disables registry reflection for 32bit processes running on a 64bit OS.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +"\n" +"Will generally raise NotImplemented if executed on a 32bit OS.\n" +"\n" +"If the key is not on the reflection list, the function succeeds but has\n" +"no effect. Disabling reflection for a key does not affect reflection\n" +"of any subkeys."); + +#define WINREG_DISABLEREFLECTIONKEY_METHODDEF \ + {"DisableReflectionKey", (PyCFunction)winreg_DisableReflectionKey, METH_VARARGS, winreg_DisableReflectionKey__doc__}, + static PyObject * -PyDisableReflectionKey(PyObject *self, PyObject *args) +winreg_DisableReflectionKey_impl(PyModuleDef *module, HKEY key); + +static PyObject * +winreg_DisableReflectionKey(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; + PyObject *return_value = NULL; + HKEY key; + + if (!PyArg_ParseTuple(args, + "O&:DisableReflectionKey", + clinic_HKEY_converter, &key)) + goto exit; + return_value = winreg_DisableReflectionKey_impl(module, key); + +exit: + return return_value; +} + +static PyObject * +winreg_DisableReflectionKey_impl(PyModuleDef *module, HKEY key) +/*[clinic end generated code: checksum=4864c248d6c265529051ec060ee72a95acbad21f]*/ +{ HMODULE hMod; typedef LONG (WINAPI *RDRKFunc)(HKEY); RDRKFunc pfn = NULL; LONG rc; - if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - /* Only available on 64bit platforms, so we must load it dynamically.*/ hMod = GetModuleHandleW(L"advapi32.dll"); @@ -1631,30 +2581,70 @@ return NULL; } Py_BEGIN_ALLOW_THREADS - rc = (*pfn)(hKey); + rc = (*pfn)(key); Py_END_ALLOW_THREADS if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegDisableReflectionKey"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +winreg.EnableReflectionKey + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + / + +Restores registry reflection for the specified disabled key. + +Will generally raise NotImplemented if executed on a 32bit OS. +Restoring reflection for a key does not affect reflection of any +subkeys. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_EnableReflectionKey__doc__, +"EnableReflectionKey(key)\n" +"Restores registry reflection for the specified disabled key.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +"\n" +"Will generally raise NotImplemented if executed on a 32bit OS.\n" +"Restoring reflection for a key does not affect reflection of any\n" +"subkeys."); + +#define WINREG_ENABLEREFLECTIONKEY_METHODDEF \ + {"EnableReflectionKey", (PyCFunction)winreg_EnableReflectionKey, METH_VARARGS, winreg_EnableReflectionKey__doc__}, + static PyObject * -PyEnableReflectionKey(PyObject *self, PyObject *args) +winreg_EnableReflectionKey_impl(PyModuleDef *module, HKEY key); + +static PyObject * +winreg_EnableReflectionKey(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; + PyObject *return_value = NULL; + HKEY key; + + if (!PyArg_ParseTuple(args, + "O&:EnableReflectionKey", + clinic_HKEY_converter, &key)) + goto exit; + return_value = winreg_EnableReflectionKey_impl(module, key); + +exit: + return return_value; +} + +static PyObject * +winreg_EnableReflectionKey_impl(PyModuleDef *module, HKEY key) +/*[clinic end generated code: checksum=ac0d21b8f636c6758e4684bd6107901c54a305bc]*/ +{ HMODULE hMod; typedef LONG (WINAPI *RERKFunc)(HKEY); RERKFunc pfn = NULL; LONG rc; - if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - /* Only available on 64bit platforms, so we must load it dynamically.*/ hMod = GetModuleHandleW(L"advapi32.dll"); @@ -1667,31 +2657,67 @@ return NULL; } Py_BEGIN_ALLOW_THREADS - rc = (*pfn)(hKey); + rc = (*pfn)(key); Py_END_ALLOW_THREADS if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnableReflectionKey"); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +winreg.QueryReflectionKey + + key: HKEY + An already open key, or any one of the predefined HKEY_* constants. + / + +Returns the reflection state for the specified key as a bool. + +Will generally raise NotImplemented if executed on a 32bit OS. +[clinic start generated code]*/ + +PyDoc_STRVAR(winreg_QueryReflectionKey__doc__, +"QueryReflectionKey(key)\n" +"Returns the reflection state for the specified key as a bool.\n" +"\n" +" key\n" +" An already open key, or any one of the predefined HKEY_* constants.\n" +"\n" +"Will generally raise NotImplemented if executed on a 32bit OS."); + +#define WINREG_QUERYREFLECTIONKEY_METHODDEF \ + {"QueryReflectionKey", (PyCFunction)winreg_QueryReflectionKey, METH_VARARGS, winreg_QueryReflectionKey__doc__}, + static PyObject * -PyQueryReflectionKey(PyObject *self, PyObject *args) +winreg_QueryReflectionKey_impl(PyModuleDef *module, HKEY key); + +static PyObject * +winreg_QueryReflectionKey(PyModuleDef *module, PyObject *args) { - HKEY hKey; - PyObject *obKey; + PyObject *return_value = NULL; + HKEY key; + + if (!PyArg_ParseTuple(args, + "O&:QueryReflectionKey", + clinic_HKEY_converter, &key)) + goto exit; + return_value = winreg_QueryReflectionKey_impl(module, key); + +exit: + return return_value; +} + +static PyObject * +winreg_QueryReflectionKey_impl(PyModuleDef *module, HKEY key) +/*[clinic end generated code: checksum=2944f54c9f2d46c0c9c7bd5a0d0a7dfccd78ce1c]*/ +{ HMODULE hMod; typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *); RQRKFunc pfn = NULL; BOOL result; LONG rc; - if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey)) - return NULL; - if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) - return NULL; - /* Only available on 64bit platforms, so we must load it dynamically.*/ hMod = GetModuleHandleW(L"advapi32.dll"); @@ -1704,7 +2730,7 @@ return NULL; } Py_BEGIN_ALLOW_THREADS - rc = (*pfn)(hKey, &result); + rc = (*pfn)(key, &result); Py_END_ALLOW_THREADS if (rc != ERROR_SUCCESS) return PyErr_SetFromWindowsErrWithFunction(rc, @@ -1713,34 +2739,29 @@ } static struct PyMethodDef winreg_methods[] = { - {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc}, - {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc}, - {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc}, - {"CreateKeyEx", (PyCFunction)PyCreateKeyEx, - METH_VARARGS | METH_KEYWORDS, CreateKeyEx_doc}, - {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc}, - {"DeleteKeyEx", (PyCFunction)PyDeleteKeyEx, - METH_VARARGS | METH_KEYWORDS, DeleteKeyEx_doc}, - {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc}, - {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc}, - {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc}, - {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc}, - {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc}, - {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS, - ExpandEnvironmentStrings_doc }, - {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc}, - {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc}, - {"OpenKey", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS, - OpenKey_doc}, - {"OpenKeyEx", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS, - OpenKeyEx_doc}, - {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc}, - {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc}, - {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc}, - {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc}, - {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc}, - {"SetValue", PySetValue, METH_VARARGS, SetValue_doc}, - {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc}, + WINREG_CLOSEKEY_METHODDEF + WINREG_CONNECTREGISTRY_METHODDEF + WINREG_CREATEKEY_METHODDEF + WINREG_CREATEKEYEX_METHODDEF + WINREG_DELETEKEY_METHODDEF + WINREG_DELETEKEYEX_METHODDEF + WINREG_DELETEVALUE_METHODDEF + WINREG_DISABLEREFLECTIONKEY_METHODDEF + WINREG_ENABLEREFLECTIONKEY_METHODDEF + WINREG_ENUMKEY_METHODDEF + WINREG_ENUMVALUE_METHODDEF + WINREG_EXPANDENVIRONMENTSTRINGS_METHODDEF + WINREG_FLUSHKEY_METHODDEF + WINREG_LOADKEY_METHODDEF + WINREG_OPENKEY_METHODDEF + WINREG_OPENKEYEX_METHODDEF + WINREG_QUERYVALUE_METHODDEF + WINREG_QUERYVALUEEX_METHODDEF + WINREG_QUERYINFOKEY_METHODDEF + WINREG_QUERYREFLECTIONKEY_METHODDEF + WINREG_SAVEKEY_METHODDEF + WINREG_SETVALUE_METHODDEF + WINREG_SETVALUEEX_METHODDEF NULL, }; diff -r cd3fdf21a6e4 -r 8cf882d09d3f PC/winsound.c --- a/PC/winsound.c Thu Jan 16 14:15:03 2014 -0800 +++ b/PC/winsound.c Fri Jan 17 01:38:15 2014 -0600 @@ -39,21 +39,10 @@ #include #include -PyDoc_STRVAR(sound_playsound_doc, -"PlaySound(sound, flags) - a wrapper around the Windows PlaySound API\n" -"\n" -"The sound argument can be a filename, data, or None.\n" -"For flag values, ored together, see module documentation."); - -PyDoc_STRVAR(sound_beep_doc, -"Beep(frequency, duration) - a wrapper around the Windows Beep API\n" -"\n" -"The frequency argument specifies frequency, in hertz, of the sound.\n" -"This parameter must be in the range 37 through 32,767.\n" -"The duration argument specifies the number of milliseconds.\n"); - -PyDoc_STRVAR(sound_msgbeep_doc, -"MessageBeep(x) - call Windows MessageBeep(x). x defaults to MB_OK."); +/*[clinic input] +module winsound +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ PyDoc_STRVAR(sound_module_doc, "PlaySound(sound, flags) - play a sound\n" @@ -67,79 +56,197 @@ "SND_NOSTOP - Do not interrupt any sounds currently playing\n" // Raising RuntimeError if needed "SND_NOWAIT - Return immediately if the sound driver is busy\n" // Without any errors "\n" -"Beep(frequency, duration) - Make a beep through the PC speaker."); +"Beep(frequency, duration) - Make a beep through the PC speaker.\n" +"MessageBeep(x) - Call Windows MessageBeep."); + +/*[clinic input] +winsound.PlaySound + + sound: Py_UNICODE(nullable=True) + The sound to play; a filename, data, or None. + flags: int + Flag values, ored together. See module documentation. + / + +A wrapper around the Windows PlaySound API. +[clinic start generated code]*/ + +PyDoc_STRVAR(winsound_PlaySound__doc__, +"PlaySound(sound, flags)\n" +"A wrapper around the Windows PlaySound API.\n" +"\n" +" sound\n" +" The sound to play; a filename, data, or None.\n" +" flags\n" +" Flag values, ored together. See module documentation."); + +#define WINSOUND_PLAYSOUND_METHODDEF \ + {"PlaySound", (PyCFunction)winsound_PlaySound, METH_VARARGS, winsound_PlaySound__doc__}, static PyObject * -sound_playsound(PyObject *s, PyObject *args) +winsound_PlaySound_impl(PyModuleDef *module, Py_UNICODE *sound, int flags); + +static PyObject * +winsound_PlaySound(PyModuleDef *module, PyObject *args) { - wchar_t *wsound; + PyObject *return_value = NULL; + Py_UNICODE *sound; int flags; - int ok; - if (PyArg_ParseTuple(args, "Zi:PlaySound", &wsound, &flags)) { - if (flags & SND_ASYNC && flags & SND_MEMORY) { - /* Sidestep reference counting headache; unfortunately this also - prevent SND_LOOP from memory. */ - PyErr_SetString(PyExc_RuntimeError, "Cannot play asynchronously from memory"); - return NULL; - } - Py_BEGIN_ALLOW_THREADS - ok = PlaySoundW(wsound, NULL, flags); - Py_END_ALLOW_THREADS - if (!ok) { - PyErr_SetString(PyExc_RuntimeError, "Failed to play sound"); - return NULL; - } - Py_INCREF(Py_None); - return Py_None; - } - return NULL; + if (!PyArg_ParseTuple(args, + "Zi:PlaySound", + &sound, &flags)) + goto exit; + return_value = winsound_PlaySound_impl(module, sound, flags); + +exit: + return return_value; } static PyObject * -sound_beep(PyObject *self, PyObject *args) +winsound_PlaySound_impl(PyModuleDef *module, Py_UNICODE *sound, int flags) +/*[clinic end generated code: checksum=e6b585306e24bf0b9d1b3b17cb75ec5f8fb61c5a]*/ { - int freq; - int dur; + int ok; + + if (flags & SND_ASYNC && flags & SND_MEMORY) { + /* Sidestep reference counting headache; unfortunately this also + prevent SND_LOOP from memory. */ + PyErr_SetString(PyExc_RuntimeError, "Cannot play asynchronously from memory"); + return NULL; + } + + Py_BEGIN_ALLOW_THREADS + ok = PlaySoundW(sound, NULL, flags); + Py_END_ALLOW_THREADS + if (!ok) { + PyErr_SetString(PyExc_RuntimeError, "Failed to play sound"); + return NULL; + } + Py_RETURN_NONE; +} + +/*[clinic input] +winsound.Beep + + frequency: int + Frequency of the sound in hertz. + Must be in the range 37 through 32,767. + duration: int + How long the sound should play, in milliseconds. + / + +A wrapper around the Windows Beep API. +[clinic start generated code]*/ + +PyDoc_STRVAR(winsound_Beep__doc__, +"Beep(frequency, duration)\n" +"A wrapper around the Windows Beep API.\n" +"\n" +" frequency\n" +" Frequency of the sound in hertz.\n" +" Must be in the range 37 through 32,767.\n" +" duration\n" +" How long the sound should play, in milliseconds."); + +#define WINSOUND_BEEP_METHODDEF \ + {"Beep", (PyCFunction)winsound_Beep, METH_VARARGS, winsound_Beep__doc__}, + +static PyObject * +winsound_Beep_impl(PyModuleDef *module, int frequency, int duration); + +static PyObject * +winsound_Beep(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + int frequency; + int duration; + + if (!PyArg_ParseTuple(args, + "ii:Beep", + &frequency, &duration)) + goto exit; + return_value = winsound_Beep_impl(module, frequency, duration); + +exit: + return return_value; +} + +static PyObject * +winsound_Beep_impl(PyModuleDef *module, int frequency, int duration) +/*[clinic end generated code: checksum=387eb7d09a780e7a47f17cedfd87c9b5573934c6]*/ +{ BOOL ok; - if (!PyArg_ParseTuple(args, "ii:Beep", &freq, &dur)) - return NULL; - - if (freq < 37 || freq > 32767) { + if (frequency < 37 || frequency > 32767) { PyErr_SetString(PyExc_ValueError, "frequency must be in 37 thru 32767"); return NULL; } Py_BEGIN_ALLOW_THREADS - ok = Beep(freq, dur); + ok = Beep(frequency, duration); Py_END_ALLOW_THREADS if (!ok) { PyErr_SetString(PyExc_RuntimeError,"Failed to beep"); return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; +} + +/*[clinic input] +winsound.MessageBeep + + x: int(c_default="MB_OK") = winsound.MB_OK + / + +Call Windows MessageBeep(x). + +x defaults to MB_OK. +[clinic start generated code]*/ + +PyDoc_STRVAR(winsound_MessageBeep__doc__, +"MessageBeep(x=winsound.MB_OK)\n" +"Call Windows MessageBeep(x).\n" +"\n" +"x defaults to MB_OK."); + +#define WINSOUND_MESSAGEBEEP_METHODDEF \ + {"MessageBeep", (PyCFunction)winsound_MessageBeep, METH_VARARGS, winsound_MessageBeep__doc__}, + +static PyObject * +winsound_MessageBeep_impl(PyModuleDef *module, int x); + +static PyObject * +winsound_MessageBeep(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + int x = MB_OK; + + if (!PyArg_ParseTuple(args, + "|i:MessageBeep", + &x)) + goto exit; + return_value = winsound_MessageBeep_impl(module, x); + +exit: + return return_value; } static PyObject * -sound_msgbeep(PyObject *self, PyObject *args) +winsound_MessageBeep_impl(PyModuleDef *module, int x) +/*[clinic end generated code: checksum=9649c3341e1acb1084d2551074eb92caa19ee66a]*/ { - int x = MB_OK; - if (!PyArg_ParseTuple(args, "|i:MessageBeep", &x)) - return NULL; MessageBeep(x); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static struct PyMethodDef sound_methods[] = { - {"PlaySound", sound_playsound, METH_VARARGS, sound_playsound_doc}, - {"Beep", sound_beep, METH_VARARGS, sound_beep_doc}, - {"MessageBeep", sound_msgbeep, METH_VARARGS, sound_msgbeep_doc}, + WINSOUND_PLAYSOUND_METHODDEF + WINSOUND_BEEP_METHODDEF + WINSOUND_MESSAGEBEEP_METHODDEF {NULL, NULL} };