Skip to content

Commit b50b6f9

Browse files
[3.8] gh-87604: Avoid publishing list of active per-interpreter audit hooks via the gc module (GH-99373) (GH-99661)
(cherry picked from commit 7b98207) Co-authored-by: Steve Dower <steve.dower@python.org>
1 parent 82ca283 commit b50b6f9

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

Lib/test/audit-tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ def hook(event, args):
341341
gc.get_referents(y)
342342

343343

344+
def test_not_in_gc():
345+
import gc
346+
347+
hook = lambda *a: None
348+
sys.addaudithook(hook)
349+
350+
for o in gc.get_objects():
351+
if isinstance(o, list):
352+
assert hook not in o
353+
354+
344355
if __name__ == "__main__":
345356
from test.support import suppress_msvcrt_asserts
346357

Lib/test/test_audit.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ def test_gc(self):
127127
["gc.get_objects", "gc.get_referrers", "gc.get_referents"]
128128
)
129129

130+
def test_not_in_gc(self):
131+
returncode, _, stderr = self.run_python("test_not_in_gc")
132+
if returncode:
133+
self.fail(stderr)
134+
130135

131136
if __name__ == "__main__":
132137
unittest.main()
Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
356356
if (is->audit_hooks == NULL) {
357357
return NULL;
358358
}
359+
/* Avoid having our list of hooks show up in the GC module */
360+
PyObject_GC_UnTrack(is->audit_hooks);
359361
}
360362

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy