Python 3.8+sysfunction
sys.audit()
auditing
Raises an audit event with a name and arguments: every hook added with sys.addaudithook() receives it in turn.
Syntax
sys.audit(event, *args)
With no hooks installed the call costs almost nothing, so a library can place events up front. Name them 'module.action' to avoid clashing with built-in ones (os.chdir, open, subprocess.Popen). If a hook raises, the exception comes straight out of sys.audit() — that is how an operation gets vetoed, so raise the event before the action, not after.
Examples
import sys
def ban(event, args):
if event == 'bank.transfer' and args[0] > 1000:
raise RuntimeError('перевод заблокирован')
sys.addaudithook(ban)
def transfer(amount):
sys.audit('bank.transfer', amount)
return f'переведено {amount}'
print(transfer(500)) # → переведено 500
try:
transfer(5000)
except RuntimeError as e:
print(e) # → перевод заблокирован
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy