Skip to content

Commit e470803

Browse files
gh-87604: Avoid publishing list of active per-interpreter audit hooks via the gc module (GH-99373)
(cherry picked from commit 4e4b13e) Co-authored-by: Steve Dower <steve.dower@python.org>
1 parent bc2cdfc commit e470803

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

Lib/test/audit-tests.py

+11
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,17 @@ def hook(event, args):
429429
syslog.closelog()
430430

431431

432+
def test_not_in_gc():
433+
import gc
434+
435+
hook = lambda *a: None
436+
sys.addaudithook(hook)
437+
438+
for o in gc.get_objects():
439+
if isinstance(o, list):
440+
assert hook not in o
441+
442+
432443
if __name__ == "__main__":
433444
from test.support import suppress_msvcrt_asserts
434445

Lib/test/test_audit.py

+5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ def test_syslog(self):
195195
('syslog.closelog', '', '')]
196196
)
197197

198+
def test_not_in_gc(self):
199+
returncode, _, stderr = self.run_python("test_not_in_gc")
200+
if returncode:
201+
self.fail(stderr)
202+
198203

199204
if __name__ == "__main__":
200205
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid publishing list of active per-interpreter audit hooks via the
2+
:mod:`gc` module

Python/sysmodule.c

+2
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
462462
if (interp->audit_hooks == NULL) {
463463
return NULL;
464464
}
465+
/* Avoid having our list of hooks show up in the GC module */
466+
PyObject_GC_UnTrack(interp->audit_hooks);
465467
}
466468

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

0 commit comments

Comments
 (0)