Skip to content

Commit 7b98207

Browse files
authored
[3.9] gh-87604: Avoid publishing list of active per-interpreter audit hooks via the gc module (GH-99373) (GH-99493)
1 parent c09dba5 commit 7b98207

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
@@ -368,6 +368,17 @@ def hook(event, args):
368368
gc.get_referents(y)
369369

370370

371+
def test_not_in_gc():
372+
import gc
373+
374+
hook = lambda *a: None
375+
sys.addaudithook(hook)
376+
377+
for o in gc.get_objects():
378+
if isinstance(o, list):
379+
assert hook not in o
380+
381+
371382
if __name__ == "__main__":
372383
from test.support import suppress_msvcrt_asserts
373384

Lib/test/test_audit.py

+5
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ def test_gc(self):
132132
["gc.get_objects", "gc.get_referrers", "gc.get_referents"]
133133
)
134134

135+
def test_not_in_gc(self):
136+
returncode, _, stderr = self.run_python("test_not_in_gc")
137+
if returncode:
138+
self.fail(stderr)
139+
135140

136141
if __name__ == "__main__":
137142
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 (is->audit_hooks == NULL) {
441441
return NULL;
442442
}
443+
/* Avoid having our list of hooks show up in the GC module */
444+
PyObject_GC_UnTrack(is->audit_hooks);
443445
}
444446

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

0 commit comments

Comments
 (0)