Skip to content

Commit 27adf49

Browse files
committed
gh-90012: Add winreg.GetValue
1 parent ed73c90 commit 27adf49

File tree

5 files changed

+355
-1
lines changed

5 files changed

+355
-1
lines changed

Doc/library/winreg.rst

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,42 @@ This module offers the following functions:
420420
.. audit-event:: winreg.QueryValue key,sub_key,value_name winreg.QueryValueEx
421421

422422

423+
.. function:: GetValue(key, sub_key, value_name, flags=RRF_RT_ANY)
424+
425+
Retrieves the type and data for a specified registry value without requiring
426+
the key to be opened first.
427+
428+
*key* is an already open key, or one of the predefined
429+
:ref:`HKEY_* constants <hkey-constants>`.
430+
431+
*sub_key* is a string that holds the name of the subkey with which the value
432+
is associated. If this parameter is ``None`` or empty, the value will be read
433+
from the key specified by *key*.
434+
435+
*value_name* is a string indicating the value to query, or ``None`` for the
436+
default value of the subkey.
437+
438+
*flags* is an integer that specifies the restrictions on the value type to
439+
be queried (see :ref:`RRF_* constants <rrf-constants>`). Default is
440+
``RRF_RT_ANY``.
441+
442+
The result is a tuple of 2 items:
443+
444+
+-------+-----------------------------------------+
445+
| Index | Meaning |
446+
+=======+=========================================+
447+
| ``0`` | The value of the registry item. |
448+
+-------+-----------------------------------------+
449+
| ``1`` | An integer giving the registry type for |
450+
| | this value (see table in docs for |
451+
| | :meth:`SetValueEx`) |
452+
+-------+-----------------------------------------+
453+
454+
.. audit-event:: winreg.GetValue key,sub_key,value_name,flags winreg.GetValue
455+
456+
.. versionadded:: next
457+
458+
423459
.. function:: SaveKey(key, file_name)
424460

425461
Saves the specified key, and all its subkeys to the specified file.
@@ -751,6 +787,83 @@ For more information, see `Registry Value Types
751787
A null-terminated string.
752788

753789

790+
.. _rrf-constants:
791+
792+
RRF_* Constants
793+
+++++++++++++++
794+
795+
Registry value retrieval flags used with :func:`GetValue`.
796+
797+
.. data:: RRF_RT_ANY
798+
799+
No type restriction. All registry value types will be returned.
800+
801+
802+
.. data:: RRF_RT_DWORD
803+
804+
Restrict query to 32-bit values.
805+
806+
807+
.. data:: RRF_RT_QWORD
808+
809+
Restrict query to 64-bit values.
810+
811+
812+
.. data:: RRF_RT_REG_BINARY
813+
814+
Restrict query to binary data (REG_BINARY).
815+
816+
817+
.. data:: RRF_RT_REG_DWORD
818+
819+
Restrict query to REG_DWORD values.
820+
821+
822+
.. data:: RRF_RT_REG_QWORD
823+
824+
Restrict query to REG_QWORD values.
825+
826+
827+
.. data:: RRF_RT_REG_EXPAND_SZ
828+
829+
Restrict query to expandable strings (REG_EXPAND_SZ).
830+
831+
832+
.. data:: RRF_RT_REG_MULTI_SZ
833+
834+
Restrict query to multi-strings (REG_MULTI_SZ).
835+
836+
837+
.. data:: RRF_RT_REG_NONE
838+
839+
Restrict query to null values (REG_NONE).
840+
841+
842+
.. data:: RRF_RT_REG_SZ
843+
844+
Restrict query to null-terminated strings (REG_SZ).
845+
846+
847+
.. data:: RRF_NOEXPAND
848+
849+
Do not automatically expand environment variables in REG_EXPAND_SZ values.
850+
851+
852+
.. data:: RRF_ZEROONFAILURE
853+
854+
Set buffer contents to zeroes on failure.
855+
856+
857+
.. data:: RRF_SUBKEY_WOW6464KEY
858+
859+
Query the 64-bit registry view on 64-bit Windows.
860+
861+
862+
.. data:: RRF_SUBKEY_WOW6432KEY
863+
864+
Query the 32-bit registry view on 64-bit Windows.
865+
866+
754867
.. _handle-object:
755868

756869
Registry Handle Objects

Lib/test/test_winreg.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,41 @@ def test_delete_tree(self):
559559
with self.assertRaises(OSError):
560560
OpenKey(HKEY_CURRENT_USER, test_key_name)
561561

562+
def test_getvalue(self):
563+
test_subkey = test_key_name + "\\GetValueTest"
564+
key = CreateKey(HKEY_CURRENT_USER, test_subkey)
565+
self.addCleanup(CloseKey, key)
566+
self.addCleanup(DeleteTree, HKEY_CURRENT_USER, test_key_name)
567+
568+
SetValueEx(key, "test_string", 0, REG_SZ, "Hello World")
569+
SetValueEx(key, "test_dword", 0, REG_DWORD, 12345)
570+
SetValueEx(key, "test_binary", 0, REG_BINARY, b"binary_data")
571+
SetValueEx(key, None, 0, REG_SZ, "Default Value")
572+
573+
result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, "test_string")
574+
self.assertEqual(result, "Hello World")
575+
self.assertEqual(typ, REG_SZ)
576+
577+
result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, "test_dword")
578+
self.assertEqual(result, 12345)
579+
self.assertEqual(typ, REG_DWORD)
580+
581+
result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, "test_binary")
582+
self.assertEqual(result, b"binary_data")
583+
self.assertEqual(typ, REG_BINARY)
584+
585+
result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, None)
586+
self.assertEqual(result, "Default Value")
587+
self.assertEqual(typ, REG_SZ)
588+
589+
result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, "")
590+
self.assertEqual(result, "Default Value")
591+
self.assertEqual(typ, REG_SZ)
592+
593+
result, typ = GetValue(HKEY_CURRENT_USER, test_subkey, "test_string", RRF_RT_REG_SZ)
594+
self.assertEqual(result, "Hello World")
595+
self.assertEqual(typ, REG_SZ)
596+
562597

563598
if __name__ == "__main__":
564599
if not REMOTE_NAME:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :func:`winreg.GetValue` function to retrieve registry values without opening
2+
keys first. Includes RRF_* constants for type restriction flags.

PC/clinic/winreg.c.h

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

0 commit comments

Comments
 (0)