Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1 | /* |
Georg Brandl | 38feaf0 | 2008-05-25 07:45:51 +0000 | [diff] [blame] | 2 | winreg.c |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 3 | |
| 4 | Windows Registry access module for Python. |
| 5 | |
| 6 | * Simple registry access written by Mark Hammond in win32api |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7 | module circa 1995. |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 8 | * Bill Tutt expanded the support significantly not long after. |
| 9 | * Numerous other people have submitted patches since then. |
| 10 | * Ripped from win32api module 03-Feb-2000 by Mark Hammond, and |
| 11 | basic Unicode support added. |
| 12 | |
| 13 | */ |
| 14 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 15 | #include "Python.h" |
| 16 | #include "structmember.h" |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 17 | #include "windows.h" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 18 | |
| 19 | static BOOL PyHKEY_AsHKEY(PyObject *ob, HKEY *pRes, BOOL bNoneOK); |
| 20 | static PyObject *PyHKEY_FromHKEY(HKEY h); |
| 21 | static BOOL PyHKEY_Close(PyObject *obHandle); |
| 22 | |
| 23 | static char errNotAHandle[] = "Object is not a handle"; |
| 24 | |
| 25 | /* The win32api module reports the function name that failed, |
| 26 | but this concept is not in the Python core. |
| 27 | Hopefully it will one day, and in the meantime I dont |
| 28 | want to lose this info... |
| 29 | */ |
| 30 | #define PyErr_SetFromWindowsErrWithFunction(rc, fnname) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | PyErr_SetFromWindowsErr(rc) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 32 | |
| 33 | /* Forward declares */ |
| 34 | |
| 35 | /* Doc strings */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 36 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 37 | "This module provides access to the Windows registry API.\n" |
| 38 | "\n" |
| 39 | "Functions:\n" |
| 40 | "\n" |
| 41 | "CloseKey() - Closes a registry key.\n" |
| 42 | "ConnectRegistry() - Establishes a connection to a predefined registry handle\n" |
| 43 | " on another computer.\n" |
| 44 | "CreateKey() - Creates the specified key, or opens it if it already exists.\n" |
| 45 | "DeleteKey() - Deletes the specified key.\n" |
| 46 | "DeleteValue() - Removes a named value from the specified registry key.\n" |
| 47 | "EnumKey() - Enumerates subkeys of the specified open registry key.\n" |
| 48 | "EnumValue() - Enumerates values of the specified open registry key.\n" |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 49 | "ExpandEnvironmentStrings() - Expand the env strings in a REG_EXPAND_SZ string.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 50 | "FlushKey() - Writes all the attributes of the specified key to the registry.\n" |
| 51 | "LoadKey() - Creates a subkey under HKEY_USER or HKEY_LOCAL_MACHINE and stores\n" |
| 52 | " registration information from a specified file into that subkey.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 53 | "OpenKey() - Opens the specified key.\n" |
| 54 | "OpenKeyEx() - Alias of OpenKey().\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 55 | "QueryValue() - Retrieves the value associated with the unnamed value for a\n" |
| 56 | " specified key in the registry.\n" |
| 57 | "QueryValueEx() - Retrieves the type and data for a specified value name\n" |
| 58 | " associated with an open registry key.\n" |
| 59 | "QueryInfoKey() - Returns information about the specified key.\n" |
| 60 | "SaveKey() - Saves the specified key, and all its subkeys a file.\n" |
| 61 | "SetValue() - Associates a value with a specified key.\n" |
| 62 | "SetValueEx() - Stores data in the value field of an open registry key.\n" |
| 63 | "\n" |
| 64 | "Special objects:\n" |
| 65 | "\n" |
| 66 | "HKEYType -- type object for HKEY objects\n" |
| 67 | "error -- exception raised for Win32 errors\n" |
| 68 | "\n" |
| 69 | "Integer constants:\n" |
| 70 | "Many constants are defined - see the documentation for each function\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 71 | "to see what constants are used, and where."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 72 | |
| 73 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 74 | PyDoc_STRVAR(CloseKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 75 | "CloseKey(hkey)\n" |
| 76 | "Closes a previously opened registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 77 | "\n" |
| 78 | "The hkey argument specifies a previously opened key.\n" |
| 79 | "\n" |
| 80 | "Note that if the key is not closed using this method, it will be\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 81 | "closed when the hkey object is destroyed by Python."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 82 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 83 | PyDoc_STRVAR(ConnectRegistry_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 84 | "ConnectRegistry(computer_name, key) -> key\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 85 | "Establishes a connection to a predefined registry handle on another computer.\n" |
| 86 | "\n" |
| 87 | "computer_name is the name of the remote computer, of the form \\\\computername.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 88 | " If None, the local computer is used.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 89 | "key is the predefined handle to connect to.\n" |
| 90 | "\n" |
| 91 | "The return value is the handle of the opened key.\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 92 | "If the function fails, a WindowsError exception is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 93 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 94 | PyDoc_STRVAR(CreateKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 95 | "CreateKey(key, sub_key) -> key\n" |
| 96 | "Creates or opens the specified key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 97 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 98 | "key is an already open key, or one of the predefined HKEY_* constants.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 99 | "sub_key is a string that names the key this method opens or creates.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 100 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 101 | "If key is one of the predefined keys, sub_key may be None. In that case,\n" |
| 102 | "the handle returned is the same key handle passed in to the function.\n" |
| 103 | "\n" |
| 104 | "If the key already exists, this function opens the existing key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 105 | "\n" |
| 106 | "The return value is the handle of the opened key.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 107 | "If the function fails, a WindowsError exception is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 108 | |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 109 | PyDoc_STRVAR(CreateKeyEx_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 110 | "CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE) -> key\n" |
| 111 | "Creates or opens the specified key.\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 112 | "\n" |
| 113 | "key is an already open key, or one of the predefined HKEY_* constants\n" |
| 114 | "sub_key is a string that names the key this method opens or creates.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 115 | "reserved is a reserved integer, and must be zero. Default is zero.\n" |
| 116 | "access is an integer that specifies an access mask that describes the \n" |
| 117 | " desired security access for the key. Default is KEY_WRITE.\n" |
| 118 | "\n" |
| 119 | "If key is one of the predefined keys, sub_key may be None. In that case,\n" |
| 120 | "the handle returned is the same key handle passed in to the function.\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 121 | "\n" |
| 122 | "If the key already exists, this function opens the existing key\n" |
| 123 | "\n" |
| 124 | "The return value is the handle of the opened key.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 125 | "If the function fails, a WindowsError exception is raised."); |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 126 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 127 | PyDoc_STRVAR(DeleteKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 128 | "DeleteKey(key, sub_key)\n" |
| 129 | "Deletes the specified key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 130 | "\n" |
| 131 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 132 | "sub_key is a string that must be a subkey of the key identified by the key\n" |
| 133 | " parameter. This value must not be None, and the key may not have\n" |
| 134 | " subkeys.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 135 | "\n" |
| 136 | "This method can not delete keys with subkeys.\n" |
| 137 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 138 | "If the function succeeds, the entire key, including all of its values,\n" |
| 139 | "is removed. If the function fails, a WindowsError exception is raised."); |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 140 | |
| 141 | PyDoc_STRVAR(DeleteKeyEx_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 142 | "DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0)\n" |
| 143 | "Deletes the specified key (64-bit OS only).\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 144 | "\n" |
| 145 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 146 | "sub_key is a string that must be a subkey of the key identified by the key\n" |
| 147 | " parameter. This value must not be None, and the key may not have\n" |
| 148 | " subkeys.\n" |
| 149 | "reserved is a reserved integer, and must be zero. Default is zero.\n" |
| 150 | "access is an integer that specifies an access mask that describes the \n" |
| 151 | " desired security access for the key. Default is KEY_WOW64_64KEY.\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 152 | "\n" |
| 153 | "This method can not delete keys with subkeys.\n" |
| 154 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 155 | "If the function succeeds, the entire key, including all of its values,\n" |
| 156 | "is removed. If the function fails, a WindowsError exception is raised.\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 157 | "On unsupported Windows versions, NotImplementedError is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 158 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 159 | PyDoc_STRVAR(DeleteValue_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 160 | "DeleteValue(key, value)\n" |
| 161 | "Removes a named value from a registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 162 | "\n" |
| 163 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 164 | "value is a string that identifies the value to remove."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 165 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 166 | PyDoc_STRVAR(EnumKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 167 | "EnumKey(key, index) -> string\n" |
| 168 | "Enumerates subkeys of an open registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 169 | "\n" |
| 170 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 171 | "index is an integer that identifies the index of the key to retrieve.\n" |
| 172 | "\n" |
| 173 | "The function retrieves the name of one subkey each time it is called.\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 174 | "It is typically called repeatedly until a WindowsError exception is\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 175 | "raised, indicating no more values are available."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 176 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 177 | PyDoc_STRVAR(EnumValue_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 178 | "EnumValue(key, index) -> tuple\n" |
| 179 | "Enumerates values of an open registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 180 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 181 | "index is an integer that identifies the index of the value to retrieve.\n" |
| 182 | "\n" |
| 183 | "The function retrieves the name of one subkey each time it is called.\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 184 | "It is typically called repeatedly, until a WindowsError exception\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 185 | "is raised, indicating no more values.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 186 | "\n" |
| 187 | "The result is a tuple of 3 items:\n" |
| 188 | "value_name is a string that identifies the value.\n" |
| 189 | "value_data is an object that holds the value data, and whose type depends\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 190 | " on the underlying registry type.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 191 | "data_type is an integer that identifies the type of the value data."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 192 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 193 | PyDoc_STRVAR(ExpandEnvironmentStrings_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 194 | "ExpandEnvironmentStrings(string) -> string\n" |
| 195 | "Expand environment vars.\n"); |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 196 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 197 | PyDoc_STRVAR(FlushKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 198 | "FlushKey(key)\n" |
| 199 | "Writes all the attributes of a key to the registry.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 200 | "\n" |
| 201 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 202 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 203 | "It is not necessary to call FlushKey to change a key. Registry changes are\n" |
| 204 | "flushed to disk by the registry using its lazy flusher. Registry changes are\n" |
| 205 | "also flushed to disk at system shutdown. Unlike CloseKey(), the FlushKey()\n" |
| 206 | "method returns only when all the data has been written to the registry.\n" |
| 207 | "\n" |
| 208 | "An application should only call FlushKey() if it requires absolute certainty\n" |
| 209 | "that registry changes are on disk. If you don't know whether a FlushKey()\n" |
| 210 | "call is required, it probably isn't."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 211 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 212 | PyDoc_STRVAR(LoadKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 213 | "LoadKey(key, sub_key, file_name)\n" |
| 214 | "Creates a subkey under the specified key and stores registration information\n" |
| 215 | "from a specified file into that subkey.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 216 | "\n" |
| 217 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 218 | "sub_key is a string that identifies the sub_key to load.\n" |
| 219 | "file_name is the name of the file to load registry data from. This file must\n" |
| 220 | " have been created with the SaveKey() function. Under the file\n" |
| 221 | " allocation table (FAT) file system, the filename may not have an\n" |
| 222 | " extension.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 223 | "\n" |
| 224 | "A call to LoadKey() fails if the calling process does not have the\n" |
| 225 | "SE_RESTORE_PRIVILEGE privilege.\n" |
| 226 | "\n" |
| 227 | "If key is a handle returned by ConnectRegistry(), then the path specified\n" |
| 228 | "in fileName is relative to the remote computer.\n" |
| 229 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 230 | "The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 231 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 232 | PyDoc_STRVAR(OpenKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 233 | "OpenKey(key, sub_key, reserved=0, access=KEY_READ) -> key\n" |
| 234 | "Opens the specified key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 235 | "\n" |
| 236 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 237 | "sub_key is a string that identifies the sub_key to open.\n" |
| 238 | "reserved is a reserved integer, and must be zero. Default is zero.\n" |
| 239 | "access is an integer that specifies an access mask that describes the desired\n" |
| 240 | " security access for the key. Default is KEY_READ\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 241 | "\n" |
| 242 | "The result is a new handle to the specified key\n" |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 243 | "If the function fails, a WindowsError exception is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 244 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 245 | PyDoc_STRVAR(OpenKeyEx_doc, "See OpenKey()"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 247 | PyDoc_STRVAR(QueryInfoKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 248 | "QueryInfoKey(key) -> tuple\n" |
| 249 | "Returns information about a key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 250 | "\n" |
| 251 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 252 | "\n" |
| 253 | "The result is a tuple of 3 items:" |
| 254 | "An integer that identifies the number of sub keys this key has.\n" |
| 255 | "An integer that identifies the number of values this key has.\n" |
| 256 | "A long integer that identifies when the key was last modified (if available)\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 257 | " as 100's of nanoseconds since Jan 1, 1600."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 259 | PyDoc_STRVAR(QueryValue_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 260 | "QueryValue(key, sub_key) -> string\n" |
| 261 | "Retrieves the unnamed value for a key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 262 | "\n" |
| 263 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 264 | "sub_key is a string that holds the name of the subkey with which the value\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 265 | " is associated. If this parameter is None or empty, the function\n" |
| 266 | " retrieves the value set by the SetValue() method for the key\n" |
| 267 | " identified by key." |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 268 | "\n" |
| 269 | "Values in the registry have name, type, and data components. This method\n" |
| 270 | "retrieves the data for a key's first value that has a NULL name.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 271 | "But the underlying API call doesn't return the type, Lame Lame Lame, DONT USE THIS!!!"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 272 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 273 | PyDoc_STRVAR(QueryValueEx_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 274 | "QueryValueEx(key, value_name) -> (value, type_id)\n" |
| 275 | "Retrieves the type and data for a specified value name associated with an\n" |
| 276 | "open registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 277 | "\n" |
| 278 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 279 | "value_name is a string indicating the value to query"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 280 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 281 | PyDoc_STRVAR(SaveKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 282 | "SaveKey(key, file_name)\n" |
| 283 | "Saves the specified key, and all its subkeys to the specified file.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 284 | "\n" |
| 285 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 286 | "file_name is the name of the file to save registry data to. This file cannot\n" |
| 287 | " already exist. If this filename includes an extension, it cannot be\n" |
| 288 | " used on file allocation table (FAT) file systems by the LoadKey(),\n" |
| 289 | " ReplaceKey() or RestoreKey() methods.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 290 | "\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 291 | "If key represents a key on a remote computer, the path described by file_name\n" |
| 292 | "is relative to the remote computer.\n" |
| 293 | "\n" |
| 294 | "The caller of this method must possess the SeBackupPrivilege security\n" |
| 295 | "privilege. This function passes NULL for security_attributes to the API."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 296 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 297 | PyDoc_STRVAR(SetValue_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 298 | "SetValue(key, sub_key, type, value)\n" |
| 299 | "Associates a value with a specified key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 300 | "\n" |
| 301 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
| 302 | "sub_key is a string that names the subkey with which the value is associated.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 303 | "type is an integer that specifies the type of the data. Currently this must\n" |
| 304 | " be REG_SZ, meaning only strings are supported.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 305 | "value is a string that specifies the new value.\n" |
| 306 | "\n" |
| 307 | "If the key specified by the sub_key parameter does not exist, the SetValue\n" |
| 308 | "function creates it.\n" |
| 309 | "\n" |
| 310 | "Value lengths are limited by available memory. Long values (more than\n" |
| 311 | "2048 bytes) should be stored as files with the filenames stored in \n" |
| 312 | "the configuration registry. This helps the registry perform efficiently.\n" |
| 313 | "\n" |
| 314 | "The key identified by the key parameter must have been opened with\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 315 | "KEY_SET_VALUE access."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 316 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 317 | PyDoc_STRVAR(SetValueEx_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 318 | "SetValueEx(key, value_name, reserved, type, value)\n" |
| 319 | "Stores data in the value field of an open registry key.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 320 | "\n" |
| 321 | "key is an already open key, or any one of the predefined HKEY_* constants.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 322 | "value_name is a string containing the name of the value to set, or None.\n" |
| 323 | "reserved can be anything - zero is always passed to the API.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 324 | "type is an integer that specifies the type of the data. This should be one of:\n" |
| 325 | " REG_BINARY -- Binary data in any form.\n" |
| 326 | " REG_DWORD -- A 32-bit number.\n" |
| 327 | " REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format.\n" |
| 328 | " REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format.\n" |
| 329 | " REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references\n" |
| 330 | " to environment variables (for example, %PATH%).\n" |
| 331 | " REG_LINK -- A Unicode symbolic link.\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 332 | " REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by\n" |
| 333 | " two null characters. Note that Python handles this\n" |
| 334 | " termination automatically.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 335 | " REG_NONE -- No defined value type.\n" |
| 336 | " REG_RESOURCE_LIST -- A device-driver resource list.\n" |
| 337 | " REG_SZ -- A null-terminated string.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 338 | "value is a string that specifies the new value.\n" |
| 339 | "\n" |
| 340 | "This method can also set additional value and type information for the\n" |
| 341 | "specified key. The key identified by the key parameter must have been\n" |
| 342 | "opened with KEY_SET_VALUE access.\n" |
| 343 | "\n" |
| 344 | "To open the key, use the CreateKeyEx() or OpenKeyEx() methods.\n" |
| 345 | "\n" |
| 346 | "Value lengths are limited by available memory. Long values (more than\n" |
| 347 | "2048 bytes) should be stored as files with the filenames stored in \n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 348 | "the configuration registry. This helps the registry perform efficiently."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 349 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 350 | PyDoc_STRVAR(DisableReflectionKey_doc, |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 351 | "Disables registry reflection for 32-bit processes running on a 64-bit\n" |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 352 | "Operating System. Will generally raise NotImplemented if executed on\n" |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 353 | "a 32-bit Operating System.\n" |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 354 | "\n" |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 355 | "If the key is not on the reflection list, the function succeeds but has no effect.\n" |
| 356 | "Disabling reflection for a key does not affect reflection of any subkeys."); |
| 357 | |
| 358 | PyDoc_STRVAR(EnableReflectionKey_doc, |
| 359 | "Restores registry reflection for the specified disabled key.\n" |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 360 | "Will generally raise NotImplemented if executed on a 32-bit Operating System.\n" |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 361 | "Restoring reflection for a key does not affect reflection of any subkeys."); |
| 362 | |
| 363 | PyDoc_STRVAR(QueryReflectionKey_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 364 | "QueryReflectionKey(hkey) -> bool\n" |
| 365 | "Determines the reflection state for the specified key.\n" |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 366 | "Will generally raise NotImplemented if executed on a 32-bit Operating System.\n"); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 367 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 368 | /* PyHKEY docstrings */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 369 | PyDoc_STRVAR(PyHKEY_doc, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 370 | "PyHKEY Object - A Python object, representing a win32 registry key.\n" |
| 371 | "\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 372 | "This object wraps a Windows HKEY object, automatically closing it when\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 373 | "the object is destroyed. To guarantee cleanup, you can call either\n" |
| 374 | "the Close() method on the PyHKEY, or the CloseKey() method.\n" |
| 375 | "\n" |
| 376 | "All functions which accept a handle object also accept an integer - \n" |
| 377 | "however, use of the handle object is encouraged.\n" |
| 378 | "\n" |
| 379 | "Functions:\n" |
| 380 | "Close() - Closes the underlying handle.\n" |
| 381 | "Detach() - Returns the integer Win32 handle, detaching it from the object\n" |
| 382 | "\n" |
| 383 | "Properties:\n" |
| 384 | "handle - The integer Win32 handle.\n" |
| 385 | "\n" |
| 386 | "Operations:\n" |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 387 | "__bool__ - Handles with an open object return true, otherwise false.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 388 | "__int__ - Converting a handle to an integer returns the Win32 handle.\n" |
Mark Dickinson | 211c625 | 2009-02-01 10:28:51 +0000 | [diff] [blame] | 389 | "rich comparison - Handle objects are compared using the handle value."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 390 | |
| 391 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 392 | PyDoc_STRVAR(PyHKEY_Close_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 393 | "key.Close()\n" |
| 394 | "Closes the underlying Windows handle.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 395 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 396 | "If the handle is already closed, no error is raised."); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 397 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 398 | PyDoc_STRVAR(PyHKEY_Detach_doc, |
Brian Curtin | e9aeca7 | 2012-10-29 18:16:39 -0500 | [diff] [blame] | 399 | "key.Detach() -> int\n" |
| 400 | "Detaches the Windows handle from the handle object.\n" |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 401 | "\n" |
| 402 | "The result is the value of the handle before it is detached. If the\n" |
| 403 | "handle is already detached, this will return zero.\n" |
| 404 | "\n" |
| 405 | "After calling this function, the handle is effectively invalidated,\n" |
| 406 | "but the handle is not closed. You would call this function when you\n" |
| 407 | "need the underlying win32 handle to exist beyond the lifetime of the\n" |
Mark Hammond | b422f95 | 2000-06-09 06:01:47 +0000 | [diff] [blame] | 408 | "handle object.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 409 | "On 64 bit windows, the result of this function is a long integer"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 410 | |
| 411 | |
| 412 | /************************************************************************ |
| 413 | |
| 414 | The PyHKEY object definition |
| 415 | |
| 416 | ************************************************************************/ |
| 417 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | PyObject_VAR_HEAD |
| 419 | HKEY hkey; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 420 | } PyHKEYObject; |
| 421 | |
| 422 | #define PyHKEY_Check(op) ((op)->ob_type == &PyHKEY_Type) |
| 423 | |
| 424 | static char *failMsg = "bad operand type"; |
| 425 | |
| 426 | static PyObject * |
| 427 | PyHKEY_unaryFailureFunc(PyObject *ob) |
| 428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 430 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 431 | } |
| 432 | static PyObject * |
| 433 | PyHKEY_binaryFailureFunc(PyObject *ob1, PyObject *ob2) |
| 434 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 436 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 437 | } |
| 438 | static PyObject * |
| 439 | PyHKEY_ternaryFailureFunc(PyObject *ob1, PyObject *ob2, PyObject *ob3) |
| 440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | PyErr_SetString(PyExc_TypeError, failMsg); |
| 442 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static void |
| 446 | PyHKEY_deallocFunc(PyObject *ob) |
| 447 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | /* Can not call PyHKEY_Close, as the ob->tp_type |
| 449 | has already been cleared, thus causing the type |
| 450 | check to fail! |
| 451 | */ |
| 452 | PyHKEYObject *obkey = (PyHKEYObject *)ob; |
| 453 | if (obkey->hkey) |
| 454 | RegCloseKey((HKEY)obkey->hkey); |
| 455 | PyObject_DEL(ob); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 459 | PyHKEY_boolFunc(PyObject *ob) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | return ((PyHKEYObject *)ob)->hkey != 0; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static PyObject * |
| 465 | PyHKEY_intFunc(PyObject *ob) |
| 466 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | PyHKEYObject *pyhkey = (PyHKEYObject *)ob; |
| 468 | return PyLong_FromVoidPtr(pyhkey->hkey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 471 | static PyObject * |
| 472 | PyHKEY_strFunc(PyObject *ob) |
| 473 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | PyHKEYObject *pyhkey = (PyHKEYObject *)ob; |
| 475 | return PyUnicode_FromFormat("<PyHKEY:%p>", pyhkey->hkey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static int |
| 479 | PyHKEY_compareFunc(PyObject *ob1, PyObject *ob2) |
| 480 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1; |
| 482 | PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2; |
| 483 | return pyhkey1 == pyhkey2 ? 0 : |
| 484 | (pyhkey1 < pyhkey2 ? -1 : 1); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Antoine Pitrou | fbb1c61 | 2010-10-23 17:37:54 +0000 | [diff] [blame] | 487 | static Py_hash_t |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 488 | PyHKEY_hashFunc(PyObject *ob) |
| 489 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | /* Just use the address. |
| 491 | XXX - should we use the handle value? |
| 492 | */ |
| 493 | return _Py_HashPointer(ob); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
| 497 | static PyNumberMethods PyHKEY_NumberMethods = |
| 498 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | PyHKEY_binaryFailureFunc, /* nb_add */ |
| 500 | PyHKEY_binaryFailureFunc, /* nb_subtract */ |
| 501 | PyHKEY_binaryFailureFunc, /* nb_multiply */ |
| 502 | PyHKEY_binaryFailureFunc, /* nb_remainder */ |
| 503 | PyHKEY_binaryFailureFunc, /* nb_divmod */ |
| 504 | PyHKEY_ternaryFailureFunc, /* nb_power */ |
| 505 | PyHKEY_unaryFailureFunc, /* nb_negative */ |
| 506 | PyHKEY_unaryFailureFunc, /* nb_positive */ |
| 507 | PyHKEY_unaryFailureFunc, /* nb_absolute */ |
| 508 | PyHKEY_boolFunc, /* nb_bool */ |
| 509 | PyHKEY_unaryFailureFunc, /* nb_invert */ |
| 510 | PyHKEY_binaryFailureFunc, /* nb_lshift */ |
| 511 | PyHKEY_binaryFailureFunc, /* nb_rshift */ |
| 512 | PyHKEY_binaryFailureFunc, /* nb_and */ |
| 513 | PyHKEY_binaryFailureFunc, /* nb_xor */ |
| 514 | PyHKEY_binaryFailureFunc, /* nb_or */ |
| 515 | PyHKEY_intFunc, /* nb_int */ |
| 516 | 0, /* nb_reserved */ |
| 517 | PyHKEY_unaryFailureFunc, /* nb_float */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 518 | }; |
| 519 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 520 | static PyObject *PyHKEY_CloseMethod(PyObject *self, PyObject *args); |
| 521 | static PyObject *PyHKEY_DetachMethod(PyObject *self, PyObject *args); |
| 522 | static PyObject *PyHKEY_Enter(PyObject *self); |
| 523 | static PyObject *PyHKEY_Exit(PyObject *self, PyObject *args); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 524 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 525 | static struct PyMethodDef PyHKEY_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | {"Close", PyHKEY_CloseMethod, METH_VARARGS, PyHKEY_Close_doc}, |
| 527 | {"Detach", PyHKEY_DetachMethod, METH_VARARGS, PyHKEY_Detach_doc}, |
| 528 | {"__enter__", (PyCFunction)PyHKEY_Enter, METH_NOARGS, NULL}, |
| 529 | {"__exit__", PyHKEY_Exit, METH_VARARGS, NULL}, |
| 530 | {NULL} |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 531 | }; |
| 532 | |
| 533 | #define OFF(e) offsetof(PyHKEYObject, e) |
| 534 | static PyMemberDef PyHKEY_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | {"handle", T_INT, OFF(hkey), READONLY}, |
| 536 | {NULL} /* Sentinel */ |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 537 | }; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 538 | |
| 539 | /* The type itself */ |
| 540 | PyTypeObject PyHKEY_Type = |
| 541 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | PyVarObject_HEAD_INIT(0, 0) /* fill in type at module init */ |
| 543 | "PyHKEY", |
| 544 | sizeof(PyHKEYObject), |
| 545 | 0, |
| 546 | PyHKEY_deallocFunc, /* tp_dealloc */ |
| 547 | 0, /* tp_print */ |
| 548 | 0, /* tp_getattr */ |
| 549 | 0, /* tp_setattr */ |
| 550 | 0, /* tp_reserved */ |
| 551 | 0, /* tp_repr */ |
| 552 | &PyHKEY_NumberMethods, /* tp_as_number */ |
| 553 | 0, /* tp_as_sequence */ |
| 554 | 0, /* tp_as_mapping */ |
| 555 | PyHKEY_hashFunc, /* tp_hash */ |
| 556 | 0, /* tp_call */ |
| 557 | PyHKEY_strFunc, /* tp_str */ |
| 558 | 0, /* tp_getattro */ |
| 559 | 0, /* tp_setattro */ |
| 560 | 0, /* tp_as_buffer */ |
| 561 | 0, /* tp_flags */ |
| 562 | PyHKEY_doc, /* tp_doc */ |
| 563 | 0, /*tp_traverse*/ |
| 564 | 0, /*tp_clear*/ |
| 565 | 0, /*tp_richcompare*/ |
| 566 | 0, /*tp_weaklistoffset*/ |
| 567 | 0, /*tp_iter*/ |
| 568 | 0, /*tp_iternext*/ |
| 569 | PyHKEY_methods, /*tp_methods*/ |
| 570 | PyHKEY_memberlist, /*tp_members*/ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 571 | }; |
| 572 | |
| 573 | /************************************************************************ |
| 574 | |
| 575 | The PyHKEY object methods |
| 576 | |
| 577 | ************************************************************************/ |
| 578 | static PyObject * |
| 579 | PyHKEY_CloseMethod(PyObject *self, PyObject *args) |
| 580 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | if (!PyArg_ParseTuple(args, ":Close")) |
| 582 | return NULL; |
| 583 | if (!PyHKEY_Close(self)) |
| 584 | return NULL; |
| 585 | Py_INCREF(Py_None); |
| 586 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static PyObject * |
| 590 | PyHKEY_DetachMethod(PyObject *self, PyObject *args) |
| 591 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 592 | void* ret; |
| 593 | PyHKEYObject *pThis = (PyHKEYObject *)self; |
| 594 | if (!PyArg_ParseTuple(args, ":Detach")) |
| 595 | return NULL; |
| 596 | ret = (void*)pThis->hkey; |
| 597 | pThis->hkey = 0; |
| 598 | return PyLong_FromVoidPtr(ret); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 601 | static PyObject * |
| 602 | PyHKEY_Enter(PyObject *self) |
| 603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | Py_XINCREF(self); |
| 605 | return self; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | static PyObject * |
| 609 | PyHKEY_Exit(PyObject *self, PyObject *args) |
| 610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | if (!PyHKEY_Close(self)) |
| 612 | return NULL; |
| 613 | Py_RETURN_NONE; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 617 | /************************************************************************ |
| 618 | The public PyHKEY API (well, not public yet :-) |
| 619 | ************************************************************************/ |
| 620 | PyObject * |
| 621 | PyHKEY_New(HKEY hInit) |
| 622 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | PyHKEYObject *key = PyObject_NEW(PyHKEYObject, &PyHKEY_Type); |
| 624 | if (key) |
| 625 | key->hkey = hInit; |
| 626 | return (PyObject *)key; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | BOOL |
| 630 | PyHKEY_Close(PyObject *ob_handle) |
| 631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | LONG rc; |
| 633 | PyHKEYObject *key; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | if (!PyHKEY_Check(ob_handle)) { |
| 636 | PyErr_SetString(PyExc_TypeError, "bad operand type"); |
| 637 | return FALSE; |
| 638 | } |
| 639 | key = (PyHKEYObject *)ob_handle; |
| 640 | rc = key->hkey ? RegCloseKey((HKEY)key->hkey) : ERROR_SUCCESS; |
| 641 | key->hkey = 0; |
| 642 | if (rc != ERROR_SUCCESS) |
| 643 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 644 | return rc == ERROR_SUCCESS; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | BOOL |
| 648 | PyHKEY_AsHKEY(PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK) |
| 649 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | if (ob == Py_None) { |
| 651 | if (!bNoneOK) { |
| 652 | PyErr_SetString( |
| 653 | PyExc_TypeError, |
| 654 | "None is not a valid HKEY in this context"); |
| 655 | return FALSE; |
| 656 | } |
| 657 | *pHANDLE = (HKEY)0; |
| 658 | } |
| 659 | else if (PyHKEY_Check(ob)) { |
| 660 | PyHKEYObject *pH = (PyHKEYObject *)ob; |
| 661 | *pHANDLE = pH->hkey; |
| 662 | } |
| 663 | else if (PyLong_Check(ob)) { |
| 664 | /* We also support integers */ |
| 665 | PyErr_Clear(); |
| 666 | *pHANDLE = (HKEY)PyLong_AsVoidPtr(ob); |
| 667 | if (PyErr_Occurred()) |
| 668 | return FALSE; |
| 669 | } |
| 670 | else { |
| 671 | PyErr_SetString( |
| 672 | PyExc_TypeError, |
| 673 | "The object is not a PyHKEY object"); |
| 674 | return FALSE; |
| 675 | } |
| 676 | return TRUE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | PyObject * |
| 680 | PyHKEY_FromHKEY(HKEY h) |
| 681 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | PyHKEYObject *op; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | /* Inline PyObject_New */ |
| 685 | op = (PyHKEYObject *) PyObject_MALLOC(sizeof(PyHKEYObject)); |
| 686 | if (op == NULL) |
| 687 | return PyErr_NoMemory(); |
| 688 | PyObject_INIT(op, &PyHKEY_Type); |
| 689 | op->hkey = h; |
| 690 | return (PyObject *)op; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | |
| 694 | /************************************************************************ |
| 695 | The module methods |
| 696 | ************************************************************************/ |
| 697 | BOOL |
| 698 | PyWinObject_CloseHKEY(PyObject *obHandle) |
| 699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | BOOL ok; |
| 701 | if (PyHKEY_Check(obHandle)) { |
| 702 | ok = PyHKEY_Close(obHandle); |
| 703 | } |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 704 | #if SIZEOF_LONG >= SIZEOF_HKEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | else if (PyLong_Check(obHandle)) { |
| 706 | long rc = RegCloseKey((HKEY)PyLong_AsLong(obHandle)); |
| 707 | ok = (rc == ERROR_SUCCESS); |
| 708 | if (!ok) |
| 709 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 710 | } |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 711 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | else if (PyLong_Check(obHandle)) { |
| 713 | long rc = RegCloseKey((HKEY)PyLong_AsVoidPtr(obHandle)); |
| 714 | ok = (rc == ERROR_SUCCESS); |
| 715 | if (!ok) |
| 716 | PyErr_SetFromWindowsErrWithFunction(rc, "RegCloseKey"); |
| 717 | } |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 718 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | else { |
| 720 | PyErr_SetString( |
| 721 | PyExc_TypeError, |
| 722 | "A handle must be a HKEY object or an integer"); |
| 723 | return FALSE; |
| 724 | } |
| 725 | return ok; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | |
| 729 | /* |
| 730 | Private Helper functions for the registry interfaces |
| 731 | |
| 732 | ** Note that fixupMultiSZ and countString have both had changes |
| 733 | ** made to support "incorrect strings". The registry specification |
| 734 | ** calls for strings to be terminated with 2 null bytes. It seems |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 735 | ** some commercial packages install strings which dont conform, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 736 | ** causing this code to fail - however, "regedit" etc still work |
| 737 | ** with these strings (ie only we dont!). |
| 738 | */ |
| 739 | static void |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 740 | fixupMultiSZ(wchar_t **str, wchar_t *data, int len) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 741 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | wchar_t *P; |
| 743 | int i; |
| 744 | wchar_t *Q; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 745 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 746 | Q = data + len; |
| 747 | for (P = data, i = 0; P < Q && *P != '\0'; P++, i++) { |
| 748 | str[i] = P; |
| 749 | for(; *P != '\0'; P++) |
| 750 | ; |
| 751 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static int |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 755 | countStrings(wchar_t *data, int len) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 756 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 757 | int strings; |
| 758 | wchar_t *P; |
| 759 | wchar_t *Q = data + len; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | for (P = data, strings = 0; P < Q && *P != '\0'; P++, strings++) |
| 762 | for (; P < Q && *P != '\0'; P++) |
| 763 | ; |
| 764 | return strings; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* Convert PyObject into Registry data. |
| 768 | Allocates space as needed. */ |
| 769 | static BOOL |
| 770 | Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize) |
| 771 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 772 | Py_ssize_t i,j; |
| 773 | switch (typ) { |
| 774 | case REG_DWORD: |
| 775 | if (value != Py_None && !PyLong_Check(value)) |
| 776 | return FALSE; |
| 777 | *retDataBuf = (BYTE *)PyMem_NEW(DWORD, 1); |
| 778 | if (*retDataBuf==NULL){ |
| 779 | PyErr_NoMemory(); |
| 780 | return FALSE; |
| 781 | } |
| 782 | *retDataSize = sizeof(DWORD); |
| 783 | if (value == Py_None) { |
| 784 | DWORD zero = 0; |
| 785 | memcpy(*retDataBuf, &zero, sizeof(DWORD)); |
| 786 | } |
| 787 | else { |
Brian Curtin | 12706f2 | 2012-12-27 10:12:45 -0600 | [diff] [blame] | 788 | DWORD d = PyLong_AsUnsignedLong(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | memcpy(*retDataBuf, &d, sizeof(DWORD)); |
| 790 | } |
| 791 | break; |
| 792 | case REG_SZ: |
| 793 | case REG_EXPAND_SZ: |
| 794 | { |
| 795 | if (value == Py_None) |
| 796 | *retDataSize = 1; |
| 797 | else { |
| 798 | if (!PyUnicode_Check(value)) |
| 799 | return FALSE; |
Brian Curtin | abb3351 | 2010-08-17 20:08:40 +0000 | [diff] [blame] | 800 | *retDataSize = Py_SAFE_DOWNCAST( |
| 801 | 2 + PyUnicode_GET_DATA_SIZE(value), |
| 802 | size_t, DWORD); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | } |
| 804 | *retDataBuf = (BYTE *)PyMem_NEW(DWORD, *retDataSize); |
| 805 | if (*retDataBuf==NULL){ |
| 806 | PyErr_NoMemory(); |
| 807 | return FALSE; |
| 808 | } |
| 809 | if (value == Py_None) |
| 810 | wcscpy((wchar_t *)*retDataBuf, L""); |
| 811 | else |
| 812 | wcscpy((wchar_t *)*retDataBuf, |
| 813 | PyUnicode_AS_UNICODE(value)); |
| 814 | break; |
| 815 | } |
| 816 | case REG_MULTI_SZ: |
| 817 | { |
| 818 | DWORD size = 0; |
| 819 | wchar_t *P; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | if (value == Py_None) |
| 822 | i = 0; |
| 823 | else { |
| 824 | if (!PyList_Check(value)) |
| 825 | return FALSE; |
| 826 | i = PyList_Size(value); |
| 827 | } |
| 828 | for (j = 0; j < i; j++) |
| 829 | { |
| 830 | PyObject *t; |
| 831 | t = PyList_GET_ITEM(value, j); |
| 832 | if (!PyUnicode_Check(t)) |
| 833 | return FALSE; |
Brian Curtin | abb3351 | 2010-08-17 20:08:40 +0000 | [diff] [blame] | 834 | size += Py_SAFE_DOWNCAST(2 + PyUnicode_GET_DATA_SIZE(t), |
| 835 | size_t, DWORD); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | *retDataSize = size + 2; |
| 839 | *retDataBuf = (BYTE *)PyMem_NEW(char, |
| 840 | *retDataSize); |
| 841 | if (*retDataBuf==NULL){ |
| 842 | PyErr_NoMemory(); |
| 843 | return FALSE; |
| 844 | } |
| 845 | P = (wchar_t *)*retDataBuf; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 847 | for (j = 0; j < i; j++) |
| 848 | { |
| 849 | PyObject *t; |
| 850 | t = PyList_GET_ITEM(value, j); |
| 851 | wcscpy(P, PyUnicode_AS_UNICODE(t)); |
| 852 | P += 1 + wcslen( |
| 853 | PyUnicode_AS_UNICODE(t)); |
| 854 | } |
| 855 | /* And doubly-terminate the list... */ |
| 856 | *P = '\0'; |
| 857 | break; |
| 858 | } |
| 859 | case REG_BINARY: |
| 860 | /* ALSO handle ALL unknown data types here. Even if we can't |
| 861 | support it natively, we should handle the bits. */ |
| 862 | default: |
| 863 | if (value == Py_None) |
| 864 | *retDataSize = 0; |
| 865 | else { |
| 866 | Py_buffer view; |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | if (!PyObject_CheckBuffer(value)) { |
| 869 | PyErr_Format(PyExc_TypeError, |
| 870 | "Objects of type '%s' can not " |
| 871 | "be used as binary registry values", |
| 872 | value->ob_type->tp_name); |
| 873 | return FALSE; |
| 874 | } |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0) |
| 877 | return FALSE; |
Neal Norwitz | 1385b89 | 2007-08-26 23:07:13 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | *retDataBuf = (BYTE *)PyMem_NEW(char, view.len); |
| 880 | if (*retDataBuf==NULL){ |
| 881 | PyBuffer_Release(&view); |
| 882 | PyErr_NoMemory(); |
| 883 | return FALSE; |
| 884 | } |
Brian Curtin | abb3351 | 2010-08-17 20:08:40 +0000 | [diff] [blame] | 885 | *retDataSize = Py_SAFE_DOWNCAST(view.len, Py_ssize_t, DWORD); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | memcpy(*retDataBuf, view.buf, view.len); |
| 887 | PyBuffer_Release(&view); |
| 888 | } |
| 889 | break; |
| 890 | } |
| 891 | return TRUE; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | /* Convert Registry data into PyObject*/ |
| 895 | static PyObject * |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 896 | Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 897 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | PyObject *obData; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | switch (typ) { |
| 901 | case REG_DWORD: |
| 902 | if (retDataSize == 0) |
Brian Curtin | 172e422 | 2012-12-27 14:04:42 -0600 | [diff] [blame] | 903 | obData = PyLong_FromUnsignedLong(0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | else |
Brian Curtin | 172e422 | 2012-12-27 14:04:42 -0600 | [diff] [blame] | 905 | obData = PyLong_FromUnsignedLong(*(int *)retDataBuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | break; |
| 907 | case REG_SZ: |
| 908 | case REG_EXPAND_SZ: |
| 909 | { |
| 910 | /* the buffer may or may not have a trailing NULL */ |
| 911 | wchar_t *data = (wchar_t *)retDataBuf; |
| 912 | int len = retDataSize / 2; |
| 913 | if (retDataSize && data[len-1] == '\0') |
| 914 | retDataSize -= 2; |
| 915 | if (retDataSize <= 0) |
| 916 | data = L""; |
| 917 | obData = PyUnicode_FromUnicode(data, retDataSize/2); |
| 918 | break; |
| 919 | } |
| 920 | case REG_MULTI_SZ: |
| 921 | if (retDataSize == 0) |
| 922 | obData = PyList_New(0); |
| 923 | else |
| 924 | { |
| 925 | int index = 0; |
| 926 | wchar_t *data = (wchar_t *)retDataBuf; |
| 927 | int len = retDataSize / 2; |
| 928 | int s = countStrings(data, len); |
| 929 | wchar_t **str = (wchar_t **)malloc(sizeof(wchar_t *)*s); |
| 930 | if (str == NULL) |
| 931 | return PyErr_NoMemory(); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | fixupMultiSZ(str, data, len); |
| 934 | obData = PyList_New(s); |
| 935 | if (obData == NULL) |
| 936 | return NULL; |
| 937 | for (index = 0; index < s; index++) |
| 938 | { |
| 939 | size_t len = wcslen(str[index]); |
| 940 | if (len > INT_MAX) { |
| 941 | PyErr_SetString(PyExc_OverflowError, |
| 942 | "registry string is too long for a Python string"); |
| 943 | Py_DECREF(obData); |
| 944 | return NULL; |
| 945 | } |
| 946 | PyList_SetItem(obData, |
| 947 | index, |
| 948 | PyUnicode_FromUnicode(str[index], len)); |
| 949 | } |
| 950 | free(str); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | break; |
| 953 | } |
| 954 | case REG_BINARY: |
| 955 | /* ALSO handle ALL unknown data types here. Even if we can't |
| 956 | support it natively, we should handle the bits. */ |
| 957 | default: |
| 958 | if (retDataSize == 0) { |
| 959 | Py_INCREF(Py_None); |
| 960 | obData = Py_None; |
| 961 | } |
| 962 | else |
| 963 | obData = PyBytes_FromStringAndSize( |
| 964 | (char *)retDataBuf, retDataSize); |
| 965 | break; |
| 966 | } |
| 967 | return obData; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /* The Python methods */ |
| 971 | |
| 972 | static PyObject * |
| 973 | PyCloseKey(PyObject *self, PyObject *args) |
| 974 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | PyObject *obKey; |
| 976 | if (!PyArg_ParseTuple(args, "O:CloseKey", &obKey)) |
| 977 | return NULL; |
| 978 | if (!PyHKEY_Close(obKey)) |
| 979 | return NULL; |
| 980 | Py_INCREF(Py_None); |
| 981 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static PyObject * |
| 985 | PyConnectRegistry(PyObject *self, PyObject *args) |
| 986 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | HKEY hKey; |
| 988 | PyObject *obKey; |
| 989 | wchar_t *szCompName = NULL; |
| 990 | HKEY retKey; |
| 991 | long rc; |
| 992 | if (!PyArg_ParseTuple(args, "ZO:ConnectRegistry", &szCompName, &obKey)) |
| 993 | return NULL; |
| 994 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 995 | return NULL; |
| 996 | Py_BEGIN_ALLOW_THREADS |
| 997 | rc = RegConnectRegistryW(szCompName, hKey, &retKey); |
| 998 | Py_END_ALLOW_THREADS |
| 999 | if (rc != ERROR_SUCCESS) |
| 1000 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1001 | "ConnectRegistry"); |
| 1002 | return PyHKEY_FromHKEY(retKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | static PyObject * |
| 1006 | PyCreateKey(PyObject *self, PyObject *args) |
| 1007 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | HKEY hKey; |
| 1009 | PyObject *obKey; |
| 1010 | wchar_t *subKey; |
| 1011 | HKEY retKey; |
| 1012 | long rc; |
| 1013 | if (!PyArg_ParseTuple(args, "OZ:CreateKey", &obKey, &subKey)) |
| 1014 | return NULL; |
| 1015 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1016 | return NULL; |
| 1017 | rc = RegCreateKeyW(hKey, subKey, &retKey); |
| 1018 | if (rc != ERROR_SUCCESS) |
| 1019 | return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKey"); |
| 1020 | return PyHKEY_FromHKEY(retKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | static PyObject * |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1024 | PyCreateKeyEx(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1025 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1026 | HKEY hKey; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1027 | PyObject *key; |
| 1028 | wchar_t *sub_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | HKEY retKey; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1030 | int reserved = 0; |
| 1031 | REGSAM access = KEY_WRITE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | long rc; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1033 | |
| 1034 | char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL}; |
| 1035 | |
| 1036 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:CreateKeyEx", kwlist, |
| 1037 | &key, &sub_key, &reserved, &access)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | return NULL; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1039 | if (!PyHKEY_AsHKEY(key, &hKey, FALSE)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | return NULL; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1041 | |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1042 | rc = RegCreateKeyExW(hKey, sub_key, reserved, NULL, (DWORD)NULL, |
| 1043 | access, NULL, &retKey, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | if (rc != ERROR_SUCCESS) |
| 1045 | return PyErr_SetFromWindowsErrWithFunction(rc, "CreateKeyEx"); |
| 1046 | return PyHKEY_FromHKEY(retKey); |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | static PyObject * |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1050 | PyDeleteKey(PyObject *self, PyObject *args) |
| 1051 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | HKEY hKey; |
| 1053 | PyObject *obKey; |
| 1054 | wchar_t *subKey; |
| 1055 | long rc; |
| 1056 | if (!PyArg_ParseTuple(args, "Ou:DeleteKey", &obKey, &subKey)) |
| 1057 | return NULL; |
| 1058 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1059 | return NULL; |
| 1060 | rc = RegDeleteKeyW(hKey, subKey ); |
| 1061 | if (rc != ERROR_SUCCESS) |
| 1062 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKey"); |
| 1063 | Py_INCREF(Py_None); |
| 1064 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | static PyObject * |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1068 | PyDeleteKeyEx(PyObject *self, PyObject *args, PyObject *kwargs) |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1069 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | HKEY hKey; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1071 | PyObject *key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | HMODULE hMod; |
| 1073 | typedef LONG (WINAPI *RDKEFunc)(HKEY, const wchar_t*, REGSAM, int); |
| 1074 | RDKEFunc pfn = NULL; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1075 | wchar_t *sub_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | long rc; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1077 | int reserved = 0; |
| 1078 | REGSAM access = KEY_WOW64_64KEY; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1079 | |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1080 | char *kwlist[] = {"key", "sub_key", "access", "reserved", NULL}; |
| 1081 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ou|ii:DeleteKeyEx", kwlist, |
| 1082 | &key, &sub_key, &access, &reserved)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | return NULL; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1084 | if (!PyHKEY_AsHKEY(key, &hKey, FALSE)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | return NULL; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | /* Only available on 64bit platforms, so we must load it |
| 1088 | dynamically. */ |
| 1089 | hMod = GetModuleHandle("advapi32.dll"); |
| 1090 | if (hMod) |
| 1091 | pfn = (RDKEFunc)GetProcAddress(hMod, |
| 1092 | "RegDeleteKeyExW"); |
| 1093 | if (!pfn) { |
| 1094 | PyErr_SetString(PyExc_NotImplementedError, |
| 1095 | "not implemented on this platform"); |
| 1096 | return NULL; |
| 1097 | } |
| 1098 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1099 | rc = (*pfn)(hKey, sub_key, access, reserved); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | Py_END_ALLOW_THREADS |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1102 | if (rc != ERROR_SUCCESS) |
| 1103 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteKeyEx"); |
| 1104 | Py_INCREF(Py_None); |
| 1105 | return Py_None; |
Brian Curtin | 3035c39 | 2010-04-21 23:56:21 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | static PyObject * |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1109 | PyDeleteValue(PyObject *self, PyObject *args) |
| 1110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | HKEY hKey; |
| 1112 | PyObject *obKey; |
| 1113 | wchar_t *subKey; |
| 1114 | long rc; |
| 1115 | if (!PyArg_ParseTuple(args, "OZ:DeleteValue", &obKey, &subKey)) |
| 1116 | return NULL; |
| 1117 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1118 | return NULL; |
| 1119 | Py_BEGIN_ALLOW_THREADS |
| 1120 | rc = RegDeleteValueW(hKey, subKey); |
| 1121 | Py_END_ALLOW_THREADS |
| 1122 | if (rc !=ERROR_SUCCESS) |
| 1123 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1124 | "RegDeleteValue"); |
| 1125 | Py_INCREF(Py_None); |
| 1126 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | static PyObject * |
| 1130 | PyEnumKey(PyObject *self, PyObject *args) |
| 1131 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | HKEY hKey; |
| 1133 | PyObject *obKey; |
| 1134 | int index; |
| 1135 | long rc; |
| 1136 | PyObject *retStr; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1137 | |
| 1138 | /* The Windows docs claim that the max key name length is 255 |
| 1139 | * characters, plus a terminating nul character. However, |
| 1140 | * empirical testing demonstrates that it is possible to |
| 1141 | * create a 256 character key that is missing the terminating |
| 1142 | * nul. RegEnumKeyEx requires a 257 character buffer to |
| 1143 | * retrieve such a key name. */ |
| 1144 | wchar_t tmpbuf[257]; |
Kristján Valur Jónsson | 984dfa7 | 2012-04-02 15:23:29 +0000 | [diff] [blame] | 1145 | DWORD len = sizeof(tmpbuf)/sizeof(wchar_t); /* includes NULL terminator */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index)) |
| 1148 | return NULL; |
| 1149 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1150 | return NULL; |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 1151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | Py_BEGIN_ALLOW_THREADS |
| 1153 | rc = RegEnumKeyExW(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL); |
| 1154 | Py_END_ALLOW_THREADS |
| 1155 | if (rc != ERROR_SUCCESS) |
| 1156 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx"); |
Tim Peters | 313fcd4 | 2006-02-19 04:05:39 +0000 | [diff] [blame] | 1157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1158 | retStr = PyUnicode_FromUnicode(tmpbuf, len); |
| 1159 | return retStr; /* can be NULL */ |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | static PyObject * |
| 1163 | PyEnumValue(PyObject *self, PyObject *args) |
| 1164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | HKEY hKey; |
| 1166 | PyObject *obKey; |
| 1167 | int index; |
| 1168 | long rc; |
| 1169 | wchar_t *retValueBuf; |
Amaury Forgeot d'Arc | f2b69df | 2010-08-16 22:11:29 +0000 | [diff] [blame] | 1170 | BYTE *tmpBuf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | BYTE *retDataBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1172 | DWORD retValueSize, bufValueSize; |
| 1173 | DWORD retDataSize, bufDataSize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | DWORD typ; |
| 1175 | PyObject *obData; |
| 1176 | PyObject *retVal; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | if (!PyArg_ParseTuple(args, "Oi:EnumValue", &obKey, &index)) |
| 1179 | return NULL; |
| 1180 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1181 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | if ((rc = RegQueryInfoKeyW(hKey, NULL, NULL, NULL, NULL, NULL, NULL, |
| 1184 | NULL, |
| 1185 | &retValueSize, &retDataSize, NULL, NULL)) |
| 1186 | != ERROR_SUCCESS) |
| 1187 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1188 | "RegQueryInfoKey"); |
| 1189 | ++retValueSize; /* include null terminators */ |
| 1190 | ++retDataSize; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1191 | bufDataSize = retDataSize; |
| 1192 | bufValueSize = retValueSize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | retValueBuf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t) * retValueSize); |
| 1194 | if (retValueBuf == NULL) |
| 1195 | return PyErr_NoMemory(); |
| 1196 | retDataBuf = (BYTE *)PyMem_Malloc(retDataSize); |
| 1197 | if (retDataBuf == NULL) { |
| 1198 | PyMem_Free(retValueBuf); |
| 1199 | return PyErr_NoMemory(); |
| 1200 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1201 | |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1202 | while (1) { |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1203 | Py_BEGIN_ALLOW_THREADS |
| 1204 | rc = RegEnumValueW(hKey, |
| 1205 | index, |
| 1206 | retValueBuf, |
| 1207 | &retValueSize, |
| 1208 | NULL, |
| 1209 | &typ, |
| 1210 | (BYTE *)retDataBuf, |
| 1211 | &retDataSize); |
| 1212 | Py_END_ALLOW_THREADS |
| 1213 | |
| 1214 | if (rc != ERROR_MORE_DATA) |
| 1215 | break; |
| 1216 | |
| 1217 | bufDataSize *= 2; |
Amaury Forgeot d'Arc | f2b69df | 2010-08-16 22:11:29 +0000 | [diff] [blame] | 1218 | tmpBuf = (BYTE *)PyMem_Realloc(retDataBuf, bufDataSize); |
Brian Curtin | 9b7e2d1 | 2010-06-08 20:57:52 +0000 | [diff] [blame] | 1219 | if (tmpBuf == NULL) { |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1220 | PyErr_NoMemory(); |
| 1221 | retVal = NULL; |
| 1222 | goto fail; |
| 1223 | } |
Brian Curtin | 9b7e2d1 | 2010-06-08 20:57:52 +0000 | [diff] [blame] | 1224 | retDataBuf = tmpBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1225 | retDataSize = bufDataSize; |
| 1226 | retValueSize = bufValueSize; |
| 1227 | } |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | if (rc != ERROR_SUCCESS) { |
| 1230 | retVal = PyErr_SetFromWindowsErrWithFunction(rc, |
| 1231 | "PyRegEnumValue"); |
| 1232 | goto fail; |
| 1233 | } |
| 1234 | obData = Reg2Py(retDataBuf, retDataSize, typ); |
| 1235 | if (obData == NULL) { |
| 1236 | retVal = NULL; |
| 1237 | goto fail; |
| 1238 | } |
| 1239 | retVal = Py_BuildValue("uOi", retValueBuf, obData, typ); |
| 1240 | Py_DECREF(obData); |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1241 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1242 | PyMem_Free(retValueBuf); |
| 1243 | PyMem_Free(retDataBuf); |
| 1244 | return retVal; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | static PyObject * |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1248 | PyExpandEnvironmentStrings(PyObject *self, PyObject *args) |
| 1249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | Py_UNICODE *retValue = NULL; |
| 1251 | Py_UNICODE *src; |
| 1252 | DWORD retValueSize; |
| 1253 | DWORD rc; |
| 1254 | PyObject *o; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | if (!PyArg_ParseTuple(args, "u:ExpandEnvironmentStrings", &src)) |
| 1257 | return NULL; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1259 | retValueSize = ExpandEnvironmentStringsW(src, retValue, 0); |
| 1260 | if (retValueSize == 0) { |
| 1261 | return PyErr_SetFromWindowsErrWithFunction(retValueSize, |
| 1262 | "ExpandEnvironmentStrings"); |
| 1263 | } |
| 1264 | retValue = (Py_UNICODE *)PyMem_Malloc(retValueSize * sizeof(Py_UNICODE)); |
| 1265 | if (retValue == NULL) { |
| 1266 | return PyErr_NoMemory(); |
| 1267 | } |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | rc = ExpandEnvironmentStringsW(src, retValue, retValueSize); |
| 1270 | if (rc == 0) { |
| 1271 | PyMem_Free(retValue); |
| 1272 | return PyErr_SetFromWindowsErrWithFunction(retValueSize, |
| 1273 | "ExpandEnvironmentStrings"); |
| 1274 | } |
| 1275 | o = PyUnicode_FromUnicode(retValue, wcslen(retValue)); |
| 1276 | PyMem_Free(retValue); |
| 1277 | return o; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | static PyObject * |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1281 | PyFlushKey(PyObject *self, PyObject *args) |
| 1282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | HKEY hKey; |
| 1284 | PyObject *obKey; |
| 1285 | long rc; |
| 1286 | if (!PyArg_ParseTuple(args, "O:FlushKey", &obKey)) |
| 1287 | return NULL; |
| 1288 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1289 | return NULL; |
| 1290 | Py_BEGIN_ALLOW_THREADS |
| 1291 | rc = RegFlushKey(hKey); |
| 1292 | Py_END_ALLOW_THREADS |
| 1293 | if (rc != ERROR_SUCCESS) |
| 1294 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegFlushKey"); |
| 1295 | Py_INCREF(Py_None); |
| 1296 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1297 | } |
| 1298 | static PyObject * |
| 1299 | PyLoadKey(PyObject *self, PyObject *args) |
| 1300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | HKEY hKey; |
| 1302 | PyObject *obKey; |
| 1303 | wchar_t *subKey; |
| 1304 | wchar_t *fileName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1306 | long rc; |
| 1307 | if (!PyArg_ParseTuple(args, "Ouu:LoadKey", &obKey, &subKey, &fileName)) |
| 1308 | return NULL; |
| 1309 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1310 | return NULL; |
| 1311 | Py_BEGIN_ALLOW_THREADS |
| 1312 | rc = RegLoadKeyW(hKey, subKey, fileName ); |
| 1313 | Py_END_ALLOW_THREADS |
| 1314 | if (rc != ERROR_SUCCESS) |
| 1315 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegLoadKey"); |
| 1316 | Py_INCREF(Py_None); |
| 1317 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | static PyObject * |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1321 | PyOpenKey(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | HKEY hKey; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1324 | PyObject *key; |
| 1325 | wchar_t *sub_key; |
| 1326 | int reserved = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | HKEY retKey; |
| 1328 | long rc; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1329 | REGSAM access = KEY_READ; |
| 1330 | |
| 1331 | char *kwlist[] = {"key", "sub_key", "reserved", "access", NULL}; |
| 1332 | |
| 1333 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OZ|ii:OpenKey", kwlist, |
| 1334 | &key, &sub_key, &reserved, &access)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | return NULL; |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1336 | if (!PyHKEY_AsHKEY(key, &hKey, FALSE)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1340 | rc = RegOpenKeyExW(hKey, sub_key, reserved, access, &retKey); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | Py_END_ALLOW_THREADS |
| 1342 | if (rc != ERROR_SUCCESS) |
| 1343 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegOpenKeyEx"); |
| 1344 | return PyHKEY_FromHKEY(retKey); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | |
| 1348 | static PyObject * |
| 1349 | PyQueryInfoKey(PyObject *self, PyObject *args) |
| 1350 | { |
| 1351 | HKEY hKey; |
| 1352 | PyObject *obKey; |
| 1353 | long rc; |
| 1354 | DWORD nSubKeys, nValues; |
| 1355 | FILETIME ft; |
| 1356 | LARGE_INTEGER li; |
| 1357 | PyObject *l; |
| 1358 | PyObject *ret; |
| 1359 | if (!PyArg_ParseTuple(args, "O:QueryInfoKey", &obKey)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1361 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1363 | if ((rc = RegQueryInfoKey(hKey, NULL, NULL, 0, &nSubKeys, NULL, NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1364 | &nValues, NULL, NULL, NULL, &ft)) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1365 | != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegQueryInfoKey"); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1367 | li.LowPart = ft.dwLowDateTime; |
| 1368 | li.HighPart = ft.dwHighDateTime; |
| 1369 | l = PyLong_FromLongLong(li.QuadPart); |
| 1370 | if (l == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1371 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1372 | ret = Py_BuildValue("iiO", nSubKeys, nValues, l); |
| 1373 | Py_DECREF(l); |
| 1374 | return ret; |
| 1375 | } |
| 1376 | |
| 1377 | static PyObject * |
| 1378 | PyQueryValue(PyObject *self, PyObject *args) |
| 1379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | HKEY hKey; |
| 1381 | PyObject *obKey; |
| 1382 | wchar_t *subKey; |
| 1383 | long rc; |
| 1384 | PyObject *retStr; |
| 1385 | wchar_t *retBuf; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1386 | DWORD bufSize = 0; |
| 1387 | DWORD retSize = 0; |
| 1388 | wchar_t *tmp; |
Guido van Rossum | a6a38ad | 2003-11-30 22:01:43 +0000 | [diff] [blame] | 1389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1390 | if (!PyArg_ParseTuple(args, "OZ:QueryValue", &obKey, &subKey)) |
| 1391 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1394 | return NULL; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1395 | |
| 1396 | rc = RegQueryValueW(hKey, subKey, NULL, &retSize); |
| 1397 | if (rc == ERROR_MORE_DATA) |
| 1398 | retSize = 256; |
| 1399 | else if (rc != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1401 | "RegQueryValue"); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1402 | |
| 1403 | bufSize = retSize; |
| 1404 | retBuf = (wchar_t *) PyMem_Malloc(bufSize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | if (retBuf == NULL) |
| 1406 | return PyErr_NoMemory(); |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1407 | |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1408 | while (1) { |
| 1409 | retSize = bufSize; |
| 1410 | rc = RegQueryValueW(hKey, subKey, retBuf, &retSize); |
| 1411 | if (rc != ERROR_MORE_DATA) |
| 1412 | break; |
| 1413 | |
| 1414 | bufSize *= 2; |
| 1415 | tmp = (wchar_t *) PyMem_Realloc(retBuf, bufSize); |
| 1416 | if (tmp == NULL) { |
| 1417 | PyMem_Free(retBuf); |
| 1418 | return PyErr_NoMemory(); |
| 1419 | } |
| 1420 | retBuf = tmp; |
| 1421 | } |
| 1422 | |
| 1423 | if (rc != ERROR_SUCCESS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | PyMem_Free(retBuf); |
| 1425 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1426 | "RegQueryValue"); |
| 1427 | } |
Guido van Rossum | a8c360e | 2007-07-17 20:50:43 +0000 | [diff] [blame] | 1428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | retStr = PyUnicode_FromUnicode(retBuf, wcslen(retBuf)); |
| 1430 | PyMem_Free(retBuf); |
| 1431 | return retStr; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | static PyObject * |
| 1435 | PyQueryValueEx(PyObject *self, PyObject *args) |
| 1436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | HKEY hKey; |
| 1438 | PyObject *obKey; |
| 1439 | wchar_t *valueName; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | long rc; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1442 | BYTE *retBuf, *tmp; |
| 1443 | DWORD bufSize = 0, retSize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | DWORD typ; |
| 1445 | PyObject *obData; |
| 1446 | PyObject *result; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | if (!PyArg_ParseTuple(args, "OZ:QueryValueEx", &obKey, &valueName)) |
| 1449 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1452 | return NULL; |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1453 | |
| 1454 | rc = RegQueryValueExW(hKey, valueName, NULL, NULL, NULL, &bufSize); |
| 1455 | if (rc == ERROR_MORE_DATA) |
| 1456 | bufSize = 256; |
| 1457 | else if (rc != ERROR_SUCCESS) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1459 | "RegQueryValueEx"); |
| 1460 | retBuf = (BYTE *)PyMem_Malloc(bufSize); |
| 1461 | if (retBuf == NULL) |
| 1462 | return PyErr_NoMemory(); |
Brian Curtin | 6085321 | 2010-05-26 17:43:50 +0000 | [diff] [blame] | 1463 | |
| 1464 | while (1) { |
| 1465 | retSize = bufSize; |
| 1466 | rc = RegQueryValueExW(hKey, valueName, NULL, &typ, |
| 1467 | (BYTE *)retBuf, &retSize); |
| 1468 | if (rc != ERROR_MORE_DATA) |
| 1469 | break; |
| 1470 | |
| 1471 | bufSize *= 2; |
| 1472 | tmp = (char *) PyMem_Realloc(retBuf, bufSize); |
| 1473 | if (tmp == NULL) { |
| 1474 | PyMem_Free(retBuf); |
| 1475 | return PyErr_NoMemory(); |
| 1476 | } |
| 1477 | retBuf = tmp; |
| 1478 | } |
| 1479 | |
| 1480 | if (rc != ERROR_SUCCESS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | PyMem_Free(retBuf); |
| 1482 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1483 | "RegQueryValueEx"); |
| 1484 | } |
| 1485 | obData = Reg2Py(retBuf, bufSize, typ); |
| 1486 | PyMem_Free(retBuf); |
| 1487 | if (obData == NULL) |
| 1488 | return NULL; |
| 1489 | result = Py_BuildValue("Oi", obData, typ); |
| 1490 | Py_DECREF(obData); |
| 1491 | return result; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | |
| 1495 | static PyObject * |
| 1496 | PySaveKey(PyObject *self, PyObject *args) |
| 1497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | HKEY hKey; |
| 1499 | PyObject *obKey; |
| 1500 | wchar_t *fileName; |
| 1501 | LPSECURITY_ATTRIBUTES pSA = NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | long rc; |
| 1504 | if (!PyArg_ParseTuple(args, "Ou:SaveKey", &obKey, &fileName)) |
| 1505 | return NULL; |
| 1506 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1507 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1508 | /* One day we may get security into the core? |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | if (!PyWinObject_AsSECURITY_ATTRIBUTES(obSA, &pSA, TRUE)) |
| 1510 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1511 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | Py_BEGIN_ALLOW_THREADS |
| 1513 | rc = RegSaveKeyW(hKey, fileName, pSA ); |
| 1514 | Py_END_ALLOW_THREADS |
| 1515 | if (rc != ERROR_SUCCESS) |
| 1516 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSaveKey"); |
| 1517 | Py_INCREF(Py_None); |
| 1518 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | static PyObject * |
| 1522 | PySetValue(PyObject *self, PyObject *args) |
| 1523 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1524 | HKEY hKey; |
| 1525 | PyObject *obKey; |
| 1526 | wchar_t *subKey; |
| 1527 | wchar_t *str; |
| 1528 | DWORD typ; |
| 1529 | DWORD len; |
| 1530 | long rc; |
| 1531 | if (!PyArg_ParseTuple(args, "OZiu#:SetValue", |
| 1532 | &obKey, |
| 1533 | &subKey, |
| 1534 | &typ, |
| 1535 | &str, |
| 1536 | &len)) |
| 1537 | return NULL; |
| 1538 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1539 | return NULL; |
| 1540 | if (typ != REG_SZ) { |
| 1541 | PyErr_SetString(PyExc_TypeError, |
| 1542 | "Type must be winreg.REG_SZ"); |
| 1543 | return NULL; |
| 1544 | } |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1546 | Py_BEGIN_ALLOW_THREADS |
| 1547 | rc = RegSetValueW(hKey, subKey, REG_SZ, str, len+1); |
| 1548 | Py_END_ALLOW_THREADS |
| 1549 | if (rc != ERROR_SUCCESS) |
| 1550 | return PyErr_SetFromWindowsErrWithFunction(rc, "RegSetValue"); |
| 1551 | Py_INCREF(Py_None); |
| 1552 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | static PyObject * |
| 1556 | PySetValueEx(PyObject *self, PyObject *args) |
| 1557 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | HKEY hKey; |
| 1559 | PyObject *obKey; |
| 1560 | Py_UNICODE *valueName; |
| 1561 | PyObject *obRes; |
| 1562 | PyObject *value; |
| 1563 | BYTE *data; |
| 1564 | DWORD len; |
| 1565 | DWORD typ; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1567 | LONG rc; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1569 | if (!PyArg_ParseTuple(args, "OZOiO:SetValueEx", |
| 1570 | &obKey, |
| 1571 | &valueName, |
| 1572 | &obRes, |
| 1573 | &typ, |
| 1574 | &value)) |
| 1575 | return NULL; |
| 1576 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1577 | return NULL; |
| 1578 | if (!Py2Reg(value, typ, &data, &len)) |
| 1579 | { |
| 1580 | if (!PyErr_Occurred()) |
| 1581 | PyErr_SetString(PyExc_ValueError, |
| 1582 | "Could not convert the data to the specified type."); |
| 1583 | return NULL; |
| 1584 | } |
| 1585 | Py_BEGIN_ALLOW_THREADS |
| 1586 | rc = RegSetValueExW(hKey, valueName, 0, typ, data, len); |
| 1587 | Py_END_ALLOW_THREADS |
| 1588 | PyMem_DEL(data); |
| 1589 | if (rc != ERROR_SUCCESS) |
| 1590 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1591 | "RegSetValueEx"); |
| 1592 | Py_INCREF(Py_None); |
| 1593 | return Py_None; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1596 | static PyObject * |
| 1597 | PyDisableReflectionKey(PyObject *self, PyObject *args) |
| 1598 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1599 | HKEY hKey; |
| 1600 | PyObject *obKey; |
| 1601 | HMODULE hMod; |
| 1602 | typedef LONG (WINAPI *RDRKFunc)(HKEY); |
| 1603 | RDRKFunc pfn = NULL; |
| 1604 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | if (!PyArg_ParseTuple(args, "O:DisableReflectionKey", &obKey)) |
| 1607 | return NULL; |
| 1608 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1609 | return NULL; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | /* Only available on 64bit platforms, so we must load it |
| 1612 | dynamically.*/ |
| 1613 | hMod = GetModuleHandle("advapi32.dll"); |
| 1614 | if (hMod) |
| 1615 | pfn = (RDRKFunc)GetProcAddress(hMod, |
| 1616 | "RegDisableReflectionKey"); |
| 1617 | if (!pfn) { |
| 1618 | PyErr_SetString(PyExc_NotImplementedError, |
| 1619 | "not implemented on this platform"); |
| 1620 | return NULL; |
| 1621 | } |
| 1622 | Py_BEGIN_ALLOW_THREADS |
| 1623 | rc = (*pfn)(hKey); |
| 1624 | Py_END_ALLOW_THREADS |
| 1625 | if (rc != ERROR_SUCCESS) |
| 1626 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1627 | "RegDisableReflectionKey"); |
| 1628 | Py_INCREF(Py_None); |
| 1629 | return Py_None; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | static PyObject * |
| 1633 | PyEnableReflectionKey(PyObject *self, PyObject *args) |
| 1634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1635 | HKEY hKey; |
| 1636 | PyObject *obKey; |
| 1637 | HMODULE hMod; |
| 1638 | typedef LONG (WINAPI *RERKFunc)(HKEY); |
| 1639 | RERKFunc pfn = NULL; |
| 1640 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1642 | if (!PyArg_ParseTuple(args, "O:EnableReflectionKey", &obKey)) |
| 1643 | return NULL; |
| 1644 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1645 | return NULL; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1647 | /* Only available on 64bit platforms, so we must load it |
| 1648 | dynamically.*/ |
| 1649 | hMod = GetModuleHandle("advapi32.dll"); |
| 1650 | if (hMod) |
| 1651 | pfn = (RERKFunc)GetProcAddress(hMod, |
| 1652 | "RegEnableReflectionKey"); |
| 1653 | if (!pfn) { |
| 1654 | PyErr_SetString(PyExc_NotImplementedError, |
| 1655 | "not implemented on this platform"); |
| 1656 | return NULL; |
| 1657 | } |
| 1658 | Py_BEGIN_ALLOW_THREADS |
| 1659 | rc = (*pfn)(hKey); |
| 1660 | Py_END_ALLOW_THREADS |
| 1661 | if (rc != ERROR_SUCCESS) |
| 1662 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1663 | "RegEnableReflectionKey"); |
| 1664 | Py_INCREF(Py_None); |
| 1665 | return Py_None; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | static PyObject * |
| 1669 | PyQueryReflectionKey(PyObject *self, PyObject *args) |
| 1670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 | HKEY hKey; |
| 1672 | PyObject *obKey; |
| 1673 | HMODULE hMod; |
| 1674 | typedef LONG (WINAPI *RQRKFunc)(HKEY, BOOL *); |
| 1675 | RQRKFunc pfn = NULL; |
| 1676 | BOOL result; |
| 1677 | LONG rc; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | if (!PyArg_ParseTuple(args, "O:QueryReflectionKey", &obKey)) |
| 1680 | return NULL; |
| 1681 | if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE)) |
| 1682 | return NULL; |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1684 | /* Only available on 64bit platforms, so we must load it |
| 1685 | dynamically.*/ |
| 1686 | hMod = GetModuleHandle("advapi32.dll"); |
| 1687 | if (hMod) |
| 1688 | pfn = (RQRKFunc)GetProcAddress(hMod, |
| 1689 | "RegQueryReflectionKey"); |
| 1690 | if (!pfn) { |
| 1691 | PyErr_SetString(PyExc_NotImplementedError, |
| 1692 | "not implemented on this platform"); |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | Py_BEGIN_ALLOW_THREADS |
| 1696 | rc = (*pfn)(hKey, &result); |
| 1697 | Py_END_ALLOW_THREADS |
| 1698 | if (rc != ERROR_SUCCESS) |
| 1699 | return PyErr_SetFromWindowsErrWithFunction(rc, |
| 1700 | "RegQueryReflectionKey"); |
| 1701 | return PyBool_FromLong(result); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1704 | static struct PyMethodDef winreg_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1705 | {"CloseKey", PyCloseKey, METH_VARARGS, CloseKey_doc}, |
| 1706 | {"ConnectRegistry", PyConnectRegistry, METH_VARARGS, ConnectRegistry_doc}, |
| 1707 | {"CreateKey", PyCreateKey, METH_VARARGS, CreateKey_doc}, |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1708 | {"CreateKeyEx", (PyCFunction)PyCreateKeyEx, |
| 1709 | METH_VARARGS | METH_KEYWORDS, CreateKeyEx_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | {"DeleteKey", PyDeleteKey, METH_VARARGS, DeleteKey_doc}, |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1711 | {"DeleteKeyEx", (PyCFunction)PyDeleteKeyEx, |
| 1712 | METH_VARARGS | METH_KEYWORDS, DeleteKeyEx_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1713 | {"DeleteValue", PyDeleteValue, METH_VARARGS, DeleteValue_doc}, |
| 1714 | {"DisableReflectionKey", PyDisableReflectionKey, METH_VARARGS, DisableReflectionKey_doc}, |
| 1715 | {"EnableReflectionKey", PyEnableReflectionKey, METH_VARARGS, EnableReflectionKey_doc}, |
| 1716 | {"EnumKey", PyEnumKey, METH_VARARGS, EnumKey_doc}, |
| 1717 | {"EnumValue", PyEnumValue, METH_VARARGS, EnumValue_doc}, |
| 1718 | {"ExpandEnvironmentStrings", PyExpandEnvironmentStrings, METH_VARARGS, |
| 1719 | ExpandEnvironmentStrings_doc }, |
| 1720 | {"FlushKey", PyFlushKey, METH_VARARGS, FlushKey_doc}, |
| 1721 | {"LoadKey", PyLoadKey, METH_VARARGS, LoadKey_doc}, |
Brian Curtin | 1771b54 | 2010-09-27 17:56:36 +0000 | [diff] [blame] | 1722 | {"OpenKey", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS, |
| 1723 | OpenKey_doc}, |
| 1724 | {"OpenKeyEx", (PyCFunction)PyOpenKey, METH_VARARGS | METH_KEYWORDS, |
| 1725 | OpenKeyEx_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1726 | {"QueryValue", PyQueryValue, METH_VARARGS, QueryValue_doc}, |
| 1727 | {"QueryValueEx", PyQueryValueEx, METH_VARARGS, QueryValueEx_doc}, |
| 1728 | {"QueryInfoKey", PyQueryInfoKey, METH_VARARGS, QueryInfoKey_doc}, |
| 1729 | {"QueryReflectionKey",PyQueryReflectionKey,METH_VARARGS, QueryReflectionKey_doc}, |
| 1730 | {"SaveKey", PySaveKey, METH_VARARGS, SaveKey_doc}, |
| 1731 | {"SetValue", PySetValue, METH_VARARGS, SetValue_doc}, |
| 1732 | {"SetValueEx", PySetValueEx, METH_VARARGS, SetValueEx_doc}, |
| 1733 | NULL, |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1734 | }; |
| 1735 | |
| 1736 | static void |
| 1737 | insint(PyObject * d, char * name, long value) |
| 1738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | PyObject *v = PyLong_FromLong(value); |
| 1740 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1741 | PyErr_Clear(); |
| 1742 | Py_XDECREF(v); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | #define ADD_INT(val) insint(d, #val, val) |
| 1746 | |
| 1747 | static void |
| 1748 | inskey(PyObject * d, char * name, HKEY key) |
| 1749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | PyObject *v = PyLong_FromVoidPtr(key); |
| 1751 | if (!v || PyDict_SetItemString(d, name, v)) |
| 1752 | PyErr_Clear(); |
| 1753 | Py_XDECREF(v); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | #define ADD_KEY(val) inskey(d, #val, val) |
| 1757 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1758 | |
| 1759 | static struct PyModuleDef winregmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1760 | PyModuleDef_HEAD_INIT, |
| 1761 | "winreg", |
| 1762 | module_doc, |
| 1763 | -1, |
| 1764 | winreg_methods, |
| 1765 | NULL, |
| 1766 | NULL, |
| 1767 | NULL, |
| 1768 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1769 | }; |
| 1770 | |
| 1771 | PyMODINIT_FUNC PyInit_winreg(void) |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1772 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | PyObject *m, *d; |
| 1774 | m = PyModule_Create(&winregmodule); |
| 1775 | if (m == NULL) |
| 1776 | return NULL; |
| 1777 | d = PyModule_GetDict(m); |
| 1778 | PyHKEY_Type.tp_doc = PyHKEY_doc; |
| 1779 | if (PyType_Ready(&PyHKEY_Type) < 0) |
| 1780 | return NULL; |
| 1781 | Py_INCREF(&PyHKEY_Type); |
| 1782 | if (PyDict_SetItemString(d, "HKEYType", |
| 1783 | (PyObject *)&PyHKEY_Type) != 0) |
| 1784 | return NULL; |
| 1785 | Py_INCREF(PyExc_WindowsError); |
| 1786 | if (PyDict_SetItemString(d, "error", |
| 1787 | PyExc_WindowsError) != 0) |
| 1788 | return NULL; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | /* Add the relevant constants */ |
| 1791 | ADD_KEY(HKEY_CLASSES_ROOT); |
| 1792 | ADD_KEY(HKEY_CURRENT_USER); |
| 1793 | ADD_KEY(HKEY_LOCAL_MACHINE); |
| 1794 | ADD_KEY(HKEY_USERS); |
| 1795 | ADD_KEY(HKEY_PERFORMANCE_DATA); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1796 | #ifdef HKEY_CURRENT_CONFIG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1797 | ADD_KEY(HKEY_CURRENT_CONFIG); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1798 | #endif |
| 1799 | #ifdef HKEY_DYN_DATA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1800 | ADD_KEY(HKEY_DYN_DATA); |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1801 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1802 | ADD_INT(KEY_QUERY_VALUE); |
| 1803 | ADD_INT(KEY_SET_VALUE); |
| 1804 | ADD_INT(KEY_CREATE_SUB_KEY); |
| 1805 | ADD_INT(KEY_ENUMERATE_SUB_KEYS); |
| 1806 | ADD_INT(KEY_NOTIFY); |
| 1807 | ADD_INT(KEY_CREATE_LINK); |
| 1808 | ADD_INT(KEY_READ); |
| 1809 | ADD_INT(KEY_WRITE); |
| 1810 | ADD_INT(KEY_EXECUTE); |
| 1811 | ADD_INT(KEY_ALL_ACCESS); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1812 | #ifdef KEY_WOW64_64KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | ADD_INT(KEY_WOW64_64KEY); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1814 | #endif |
| 1815 | #ifdef KEY_WOW64_32KEY |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | ADD_INT(KEY_WOW64_32KEY); |
Martin v. Löwis | d218dc1 | 2008-04-07 03:17:54 +0000 | [diff] [blame] | 1817 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1818 | ADD_INT(REG_OPTION_RESERVED); |
| 1819 | ADD_INT(REG_OPTION_NON_VOLATILE); |
| 1820 | ADD_INT(REG_OPTION_VOLATILE); |
| 1821 | ADD_INT(REG_OPTION_CREATE_LINK); |
| 1822 | ADD_INT(REG_OPTION_BACKUP_RESTORE); |
| 1823 | ADD_INT(REG_OPTION_OPEN_LINK); |
| 1824 | ADD_INT(REG_LEGAL_OPTION); |
| 1825 | ADD_INT(REG_CREATED_NEW_KEY); |
| 1826 | ADD_INT(REG_OPENED_EXISTING_KEY); |
| 1827 | ADD_INT(REG_WHOLE_HIVE_VOLATILE); |
| 1828 | ADD_INT(REG_REFRESH_HIVE); |
| 1829 | ADD_INT(REG_NO_LAZY_FLUSH); |
| 1830 | ADD_INT(REG_NOTIFY_CHANGE_NAME); |
| 1831 | ADD_INT(REG_NOTIFY_CHANGE_ATTRIBUTES); |
| 1832 | ADD_INT(REG_NOTIFY_CHANGE_LAST_SET); |
| 1833 | ADD_INT(REG_NOTIFY_CHANGE_SECURITY); |
| 1834 | ADD_INT(REG_LEGAL_CHANGE_FILTER); |
| 1835 | ADD_INT(REG_NONE); |
| 1836 | ADD_INT(REG_SZ); |
| 1837 | ADD_INT(REG_EXPAND_SZ); |
| 1838 | ADD_INT(REG_BINARY); |
| 1839 | ADD_INT(REG_DWORD); |
| 1840 | ADD_INT(REG_DWORD_LITTLE_ENDIAN); |
| 1841 | ADD_INT(REG_DWORD_BIG_ENDIAN); |
| 1842 | ADD_INT(REG_LINK); |
| 1843 | ADD_INT(REG_MULTI_SZ); |
| 1844 | ADD_INT(REG_RESOURCE_LIST); |
| 1845 | ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR); |
| 1846 | ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST); |
| 1847 | return m; |
Guido van Rossum | 9f3712c | 2000-03-28 20:37:15 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Martin v. Löwis | f82d9b5 | 2007-09-03 07:43:05 +0000 | [diff] [blame] | 1850 | |