summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2021-03-10 00:53:57 (GMT)
committerGitHub <noreply@github.com>2021-03-10 00:53:57 (GMT)
commitb4f9089d4aa787c5b74134c98e5f0f11d9e63095 (patch)
tree26bfa4b05e2e5371f1473ca108b1451bcd49de6d /Lib/test
parent62a03cd490f81c0fb01eaceb31aa8a4c7800ed0e (diff)
downloadcpython-b4f9089d4aa787c5b74134c98e5f0f11d9e63095.zip
cpython-b4f9089d4aa787c5b74134c98e5f0f11d9e63095.tar.gz
cpython-b4f9089d4aa787c5b74134c98e5f0f11d9e63095.tar.bz2
bpo-43439: Add audit hooks for gc functions (GH-24794)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/audit-tests.py18
-rw-r--r--Lib/test/test_audit.py13
2 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py
index ee6fc93..8e66594 100644
--- a/Lib/test/audit-tests.py
+++ b/Lib/test/audit-tests.py
@@ -323,6 +323,24 @@ def test_socket():
sock.close()
+def test_gc():
+ import gc
+
+ def hook(event, args):
+ if event.startswith("gc."):
+ print(event, *args)
+
+ sys.addaudithook(hook)
+
+ gc.get_objects(generation=1)
+
+ x = object()
+ y = [x]
+
+ gc.get_referrers(x)
+ gc.get_referents(y)
+
+
if __name__ == "__main__":
from test.support import suppress_msvcrt_asserts
diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py
index 4f8d06a..58180e1 100644
--- a/Lib/test/test_audit.py
+++ b/Lib/test/test_audit.py
@@ -118,5 +118,18 @@ class AuditTest(unittest.TestCase):
self.assertEqual(events[2][0], "socket.bind")
self.assertTrue(events[2][2].endswith("('127.0.0.1', 8080)"))
+ def test_gc(self):
+ returncode, events, stderr = self.run_python("test_gc")
+ if returncode:
+ self.fail(stderr)
+
+ if support.verbose:
+ print(*events, sep='\n')
+ self.assertEqual(
+ [event[0] for event in events],
+ ["gc.get_objects", "gc.get_referrers", "gc.get_referents"]
+ )
+
+
if __name__ == "__main__":
unittest.main()