Skip to content

Commit

Permalink
gh-87604: Avoid publishing list of active per-interpreter audit hooks…
Browse files Browse the repository at this point in the history
… via the gc module (GH-99373)

(cherry picked from commit 4e4b13e)

Co-authored-by: Steve Dower <steve.dower@python.org>
  • Loading branch information
miss-islington and zooba authored Nov 15, 2022
1 parent bc2cdfc commit e470803
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,17 @@ def hook(event, args):
syslog.closelog()


def test_not_in_gc():
import gc

hook = lambda *a: None
sys.addaudithook(hook)

for o in gc.get_objects():
if isinstance(o, list):
assert hook not in o


if __name__ == "__main__":
from test.support import suppress_msvcrt_asserts

Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ def test_syslog(self):
('syslog.closelog', '', '')]
)

def test_not_in_gc(self):
returncode, _, stderr = self.run_python("test_not_in_gc")
if returncode:
self.fail(stderr)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Avoid publishing list of active per-interpreter audit hooks via the
:mod:`gc` module
2 changes: 2 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
if (interp->audit_hooks == NULL) {
return NULL;
}
/* Avoid having our list of hooks show up in the GC module */
PyObject_GC_UnTrack(interp->audit_hooks);
}

if (PyList_Append(interp->audit_hooks, hook) < 0) {
Expand Down

0 comments on commit e470803

Please sign in to comment.