Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Doc/c-api/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,18 @@ accessible to C code. They all work with the current interpreter thread's
arguments to this function will be consumed, using it may cause reference
leaks.)

Note that ``#`` format characters should always be treated as
``Py_ssize_t``, regardless of whether ``PY_SSIZE_T_CLEAN`` was defined.

:func:`sys.audit` performs the same function from Python code.

.. versionadded:: 3.8

.. versionchanged:: 3.8.2

Require ``Py_ssize_t`` for ``#`` format characters. Previously, an
unavoidable deprecation warning was raised.


.. c:function:: int PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)

Expand Down
49 changes: 49 additions & 0 deletions Doc/library/winreg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ This module offers the following functions:
The return value is the handle of the opened key. If the function fails, an
:exc:`OSError` exception is raised.

.. audit-event:: winreg.ConnectRegistry computer_name,key winreg.ConnectRegistry

.. versionchanged:: 3.3
See :ref:`above <exception-changed>`.

Expand All @@ -75,6 +77,10 @@ This module offers the following functions:
The return value is the handle of the opened key. If the function fails, an
:exc:`OSError` exception is raised.

.. audit-event:: winreg.CreateKey key,sub_key,access winreg.CreateKey

.. audit-event:: winreg.OpenKey/result key winreg.CreateKey

.. versionchanged:: 3.3
See :ref:`above <exception-changed>`.

Expand Down Expand Up @@ -103,6 +109,10 @@ This module offers the following functions:
The return value is the handle of the opened key. If the function fails, an
:exc:`OSError` exception is raised.

.. audit-event:: winreg.CreateKey key,sub_key,access winreg.CreateKeyEx

.. audit-event:: winreg.OpenKey/result key winreg.CreateKeyEx

.. versionadded:: 3.2

.. versionchanged:: 3.3
Expand All @@ -124,6 +134,8 @@ This module offers the following functions:
If the method succeeds, the entire key, including all of its values, is removed.
If the method fails, an :exc:`OSError` exception is raised.

.. audit-event:: winreg.DeleteKey key,sub_key,access winreg.DeleteKey

.. versionchanged:: 3.3
See :ref:`above <exception-changed>`.

Expand Down Expand Up @@ -158,6 +170,8 @@ This module offers the following functions:

On unsupported Windows versions, :exc:`NotImplementedError` is raised.

.. audit-event:: winreg.DeleteKey key,sub_key,access winreg.DeleteKeyEx

.. versionadded:: 3.2

.. versionchanged:: 3.3
Expand All @@ -173,6 +187,8 @@ This module offers the following functions:

*value* is a string that identifies the value to remove.

.. audit-event:: winreg.DeleteValue key,value winreg.DeleteValue


.. function:: EnumKey(key, index)

Expand All @@ -187,6 +203,8 @@ This module offers the following functions:
typically called repeatedly until an :exc:`OSError` exception is
raised, indicating, no more values are available.

.. audit-event:: winreg.EnumKey key,index winreg.EnumKey

.. versionchanged:: 3.3
See :ref:`above <exception-changed>`.

Expand Down Expand Up @@ -220,6 +238,8 @@ This module offers the following functions:
| | :meth:`SetValueEx`) |
+-------+--------------------------------------------+

.. audit-event:: winreg.EnumValue key,index winreg.EnumValue

.. versionchanged:: 3.3
See :ref:`above <exception-changed>`.

Expand All @@ -235,6 +255,8 @@ This module offers the following functions:
>>> ExpandEnvironmentStrings('%windir%')
'C:\\Windows'

.. audit-event:: winreg.ExpandEnvironmentStrings str winreg.ExpandEnvironmentStrings


.. function:: FlushKey(key)

Expand Down Expand Up @@ -279,6 +301,8 @@ This module offers the following functions:
If *key* is a handle returned by :func:`ConnectRegistry`, then the path
specified in *file_name* is relative to the remote computer.

.. audit-event:: winreg.LoadKey key,sub_key,file_name winreg.LoadKey


.. function:: OpenKey(key, sub_key, reserved=0, access=KEY_READ)
OpenKeyEx(key, sub_key, reserved=0, access=KEY_READ)
Expand All @@ -300,6 +324,10 @@ This module offers the following functions:

If the function fails, :exc:`OSError` is raised.

.. audit-event:: winreg.OpenKey key,sub_key,access winreg.OpenKey

.. audit-event:: winreg.OpenKey/result key winreg.OpenKey

.. versionchanged:: 3.2
Allow the use of named arguments.

Expand Down Expand Up @@ -330,6 +358,8 @@ This module offers the following functions:
| | nanoseconds since Jan 1, 1601. |
+-------+---------------------------------------------+

.. audit-event:: winreg.QueryInfoKey key winreg.QueryInfoKey


.. function:: QueryValue(key, sub_key)

Expand All @@ -347,6 +377,8 @@ This module offers the following functions:
underlying API call doesn't return the type, so always use
:func:`QueryValueEx` if possible.

.. audit-event:: winreg.QueryValue key,sub_key,value_name winreg.QueryValue


.. function:: QueryValueEx(key, value_name)

Expand All @@ -370,6 +402,8 @@ This module offers the following functions:
| | :meth:`SetValueEx`) |
+-------+-----------------------------------------+

.. audit-event:: winreg.QueryValue key,sub_key,value_name winreg.QueryValueEx


.. function:: SaveKey(key, file_name)

Expand All @@ -393,6 +427,8 @@ This module offers the following functions:

This function passes ``NULL`` for *security_attributes* to the API.

.. audit-event:: winreg.SaveKey key,file_name winreg.SaveKey


.. function:: SetValue(key, sub_key, type, value)

Expand All @@ -419,6 +455,8 @@ This module offers the following functions:
The key identified by the *key* parameter must have been opened with
:const:`KEY_SET_VALUE` access.

.. audit-event:: winreg.SetValue key,sub_key,type,value winreg.SetValue


.. function:: SetValueEx(key, value_name, reserved, type, value)

Expand Down Expand Up @@ -447,6 +485,8 @@ This module offers the following functions:
bytes) should be stored as files with the filenames stored in the configuration
registry. This helps the registry perform efficiently.

.. audit-event:: winreg.SetValue key,sub_key,type,value winreg.SetValueEx


.. function:: DisableReflectionKey(key)

Expand All @@ -463,6 +503,8 @@ This module offers the following functions:
effect. Disabling reflection for a key does not affect reflection of any
subkeys.

.. audit-event:: winreg.DisableReflectionKey key winreg.DisableReflectionKey


.. function:: EnableReflectionKey(key)

Expand All @@ -476,6 +518,8 @@ This module offers the following functions:

Restoring reflection for a key does not affect reflection of any subkeys.

.. audit-event:: winreg.EnableReflectionKey key winreg.EnableReflectionKey


.. function:: QueryReflectionKey(key)

Expand All @@ -489,6 +533,8 @@ This module offers the following functions:
Will generally raise :exc:`NotImplementedError` if executed on a 32-bit
operating system.

.. audit-event:: winreg.QueryReflectionKey key winreg.QueryReflectionKey


.. _constants:

Expand Down Expand Up @@ -741,6 +787,9 @@ integer handle, and also disconnect the Windows handle from the handle object.
handle is not closed. You would call this function when you need the
underlying Win32 handle to exist beyond the lifetime of the handle object.

.. audit-event:: winreg.PyHKEY.Detach key winreg.PyHKEY.Detach


.. method:: PyHKEY.__enter__()
PyHKEY.__exit__(\*exc_info)

Expand Down
23 changes: 23 additions & 0 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,29 @@ def hook(event, args):
write_unraisable_exc(RuntimeError("nonfatal-error"), "for audit hook test", None)


def test_winreg():
from winreg import OpenKey, EnumKey, CloseKey, HKEY_LOCAL_MACHINE

def hook(event, args):
if not event.startswith("winreg."):
return
print(event, *args)

sys.addaudithook(hook)

k = OpenKey(HKEY_LOCAL_MACHINE, "Software")
EnumKey(k, 0)
try:
EnumKey(k, 10000)
except OSError:
pass
else:
raise RuntimeError("Expected EnumKey(HKLM, 10000) to fail")

kv = k.Detach()
CloseKey(kv)


if __name__ == "__main__":
from test.libregrtest.setup import suppress_msvcrt_asserts

Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ def test_unraisablehook(self):
"RuntimeError('nonfatal-error') Exception ignored for audit hook test",
)

def test_winreg(self):
support.import_module("winreg")
returncode, events, stderr = self.run_python("test_winreg")
if returncode:
self.fail(stderr)

self.assertEqual(events[0][0], "winreg.OpenKey")
self.assertEqual(events[1][0], "winreg.OpenKey/result")
expected = events[1][2]
self.assertTrue(expected)
self.assertSequenceEqual(["winreg.EnumKey", " ", f"{expected} 0"], events[2])
self.assertSequenceEqual(["winreg.EnumKey", " ", f"{expected} 10000"], events[3])
self.assertSequenceEqual(["winreg.PyHKEY.Detach", " ", expected], events[4])


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:c:func:`PySys_Audit` now requires ``Py_ssize_t`` to be used for size
arguments in the format string, regardless of whethen ``PY_SSIZE_T_CLEAN``
was defined at include time.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add auditing events to functions in :mod:`winreg`.
Loading