Skip to content

Commit 5db7d70

Browse files
aisklkollar
authored andcommitted
pythongh-138314: Add winreg.DeleteTree (pythonGH-138388)
1 parent e9ffe17 commit 5db7d70

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

Doc/library/winreg.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,24 @@ This module offers the following functions:
173173
See :ref:`above <exception-changed>`.
174174

175175

176+
.. function:: DeleteTree(key, sub_key=None)
177+
178+
Deletes the specified key and all its subkeys and values recursively.
179+
180+
*key* is an already open key, or one of the predefined
181+
:ref:`HKEY_* constants <hkey-constants>`.
182+
183+
*sub_key* is a string that names the subkey to delete. If ``None``,
184+
deletes all subkeys and values of the specified key.
185+
186+
This function deletes a key and all its descendants. If *sub_key* is
187+
``None``, all subkeys and values of the specified key are deleted.
188+
189+
.. audit-event:: winreg.DeleteTree key,sub_key winreg.DeleteTree
190+
191+
.. versionadded:: next
192+
193+
176194
.. function:: DeleteValue(key, value)
177195

178196
Removes a named value from a registry key.

Lib/test/test_winreg.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,21 @@ def test_exception_numbers(self):
517517
with self.assertRaises(FileNotFoundError) as ctx:
518518
QueryValue(HKEY_CLASSES_ROOT, 'some_value_that_does_not_exist')
519519

520+
def test_delete_tree(self):
521+
with CreateKey(HKEY_CURRENT_USER, test_key_name) as main_key:
522+
with CreateKey(main_key, "subkey1") as subkey1:
523+
SetValueEx(subkey1, "value1", 0, REG_SZ, "test_value1")
524+
with CreateKey(subkey1, "subsubkey1") as subsubkey1:
525+
SetValueEx(subsubkey1, "value2", 0, REG_DWORD, 42)
526+
527+
with CreateKey(main_key, "subkey2") as subkey2:
528+
SetValueEx(subkey2, "value3", 0, REG_SZ, "test_value3")
529+
530+
DeleteTree(HKEY_CURRENT_USER, test_key_name)
531+
532+
with self.assertRaises(OSError):
533+
OpenKey(HKEY_CURRENT_USER, test_key_name)
534+
520535

521536
if __name__ == "__main__":
522537
if not REMOTE_NAME:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :func:`winreg.DeleteTree`.

PC/clinic/winreg.c.h

Lines changed: 69 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PC/winreg.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,45 @@ winreg_EnableReflectionKey_impl(PyObject *module, HKEY key)
20192019
Py_RETURN_NONE;
20202020
}
20212021

2022+
/*[clinic input]
2023+
winreg.DeleteTree
2024+
2025+
key: HKEY
2026+
An already open key, or any one of the predefined HKEY_* constants.
2027+
sub_key: Py_UNICODE(accept={str, NoneType}) = None
2028+
A string that names the subkey to delete. If None, deletes all subkeys
2029+
and values of the specified key.
2030+
/
2031+
2032+
Deletes the specified key and all its subkeys and values recursively.
2033+
2034+
This function deletes a key and all its descendants. If sub_key is None,
2035+
all subkeys and values of the specified key are deleted.
2036+
[clinic start generated code]*/
2037+
2038+
static PyObject *
2039+
winreg_DeleteTree_impl(PyObject *module, HKEY key, const wchar_t *sub_key)
2040+
/*[clinic end generated code: output=c34395ee59290501 input=419ef9bb8b06e4bf]*/
2041+
{
2042+
LONG rc;
2043+
2044+
if (PySys_Audit("winreg.DeleteTree", "nu",
2045+
(Py_ssize_t)key, sub_key) < 0) {
2046+
return NULL;
2047+
}
2048+
2049+
Py_BEGIN_ALLOW_THREADS
2050+
rc = RegDeleteTreeW(key, sub_key);
2051+
Py_END_ALLOW_THREADS
2052+
2053+
if (rc != ERROR_SUCCESS) {
2054+
PyErr_SetFromWindowsErrWithFunction(rc, "RegDeleteTreeW");
2055+
return NULL;
2056+
}
2057+
2058+
Py_RETURN_NONE;
2059+
}
2060+
20222061
/*[clinic input]
20232062
winreg.QueryReflectionKey
20242063
@@ -2077,6 +2116,7 @@ static struct PyMethodDef winreg_methods[] = {
20772116
WINREG_DELETEKEY_METHODDEF
20782117
WINREG_DELETEKEYEX_METHODDEF
20792118
WINREG_DELETEVALUE_METHODDEF
2119+
WINREG_DELETETREE_METHODDEF
20802120
WINREG_DISABLEREFLECTIONKEY_METHODDEF
20812121
WINREG_ENABLEREFLECTIONKEY_METHODDEF
20822122
WINREG_ENUMKEY_METHODDEF

0 commit comments

Comments
 (0)