Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a928e7

Browse files
committed
Patch #977553: speed up RegEnumKey call
1 parent 093ab1a commit 9a928e7

1 file changed

Lines changed: 12 additions & 19 deletions

File tree

PC/_winreg.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,30 +1033,23 @@ PyEnumKey(PyObject *self, PyObject *args)
10331033
long rc;
10341034
PyObject *retStr;
10351035
char *retBuf;
1036-
DWORD len;
1036+
DWORD len = 256; /* includes NULL terminator */
1037+
char tmpbuf[256]; /* max key name length is 255 */
10371038

10381039
if (!PyArg_ParseTuple(args, "Oi:EnumKey", &obKey, &index))
10391040
return NULL;
10401041
if (!PyHKEY_AsHKEY(obKey, &hKey, FALSE))
10411042
return NULL;
1042-
1043-
if ((rc = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, &len,
1044-
NULL, NULL, NULL, NULL, NULL, NULL))
1045-
!= ERROR_SUCCESS)
1046-
return PyErr_SetFromWindowsErrWithFunction(rc,
1047-
"RegQueryInfoKey");
1048-
++len; /* include null terminator */
1049-
retStr = PyString_FromStringAndSize(NULL, len);
1050-
if (retStr == NULL)
1051-
return NULL;
1052-
retBuf = PyString_AS_STRING(retStr);
1053-
1054-
if ((rc = RegEnumKey(hKey, index, retBuf, len)) != ERROR_SUCCESS) {
1055-
Py_DECREF(retStr);
1056-
return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKey");
1057-
}
1058-
_PyString_Resize(&retStr, strlen(retBuf));
1059-
return retStr;
1043+
1044+
Py_BEGIN_ALLOW_THREADS
1045+
rc = RegEnumKeyEx(hKey, index, tmpbuf, &len, NULL, NULL, NULL, NULL);
1046+
Py_END_ALLOW_THREADS
1047+
if (rc != ERROR_SUCCESS)
1048+
return PyErr_SetFromWindowsErrWithFunction(rc, "RegEnumKeyEx");
1049+
1050+
++len; /* include null terminator */
1051+
retStr = PyString_FromStringAndSize(tmpbuf, len);
1052+
return retStr; /* can be NULL */
10601053
}
10611054

10621055
static PyObject *

0 commit comments

Comments
 (0)