Skip to content

Commit f4cb828

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 e1e8a15 commit f4cb828

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
@@ -440,6 +440,17 @@ def hook(event, args):
440440
syslog.closelog()
441441

442442

443+
def test_not_in_gc():
444+
import gc
445+
446+
hook = lambda *a: None
447+
sys.addaudithook(hook)
448+
449+
for o in gc.get_objects():
450+
if isinstance(o, list):
451+
assert hook not in o
452+
453+
443454
if __name__ == "__main__":
444455
from test.support import suppress_msvcrt_asserts
445456

Lib/test/test_audit.py

+5
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ def test_syslog(self):
209209
('syslog.closelog', '', '')]
210210
)
211211

212+
def test_not_in_gc(self):
213+
returncode, _, stderr = self.run_python("test_not_in_gc")
214+
if returncode:
215+
self.fail(stderr)
216+
212217

213218
if __name__ == "__main__":
214219
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
@@ -440,6 +440,8 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
440440
if (interp->audit_hooks == NULL) {
441441
return NULL;
442442
}
443+
/* Avoid having our list of hooks show up in the GC module */
444+
PyObject_GC_UnTrack(interp->audit_hooks);
443445
}
444446

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

0 commit comments

Comments
 (0)