bpo-41162: Clear audit hooks later during finalization (GH-21222)

Co-authored-by: Konge <zkonge@outlook.com>
diff --git a/Programs/_testembed.c b/Programs/_testembed.c
index d89f6be..3bf0c37 100644
--- a/Programs/_testembed.c
+++ b/Programs/_testembed.c
@@ -1116,8 +1116,11 @@
     return result;
 }
 
+static int _audit_hook_clear_count = 0;
+
 static int _audit_hook(const char *event, PyObject *args, void *userdata)
 {
+    assert(args && PyTuple_CheckExact(args));
     if (strcmp(event, "_testembed.raise") == 0) {
         PyErr_SetString(PyExc_RuntimeError, "Intentional error");
         return -1;
@@ -1126,6 +1129,8 @@
             return -1;
         }
         return 0;
+    } else if (strcmp(event, "cpython._PySys_ClearAuditHooks") == 0) {
+        _audit_hook_clear_count += 1;
     }
     return 0;
 }
@@ -1171,6 +1176,9 @@
 {
     int result = _test_audit(42);
     Py_Finalize();
+    if (_audit_hook_clear_count != 1) {
+        return 0x1000 | _audit_hook_clear_count;
+    }
     return result;
 }