1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
import sys
try:
import __pypy__
except ImportError:
__pypy__ = None
import pytest
class TestHook:
__test__ = False
def __init__(self, raise_on_events=None, exc_type=RuntimeError):
self.raise_on_events = raise_on_events or ()
self.exc_type = exc_type
self.seen = []
self.closed = False
def __enter__(self, *a):
sys.addaudithook(self)
return self
def __exit__(self, *a):
self.close()
if hasattr(__pypy__, '_testing_clear_audithooks'):
__pypy__._testing_clear_audithooks()
def close(self):
self.closed = True
@property
def seen_events(self):
return [i[0] for i in self.seen]
def __call__(self, event, args):
if self.closed:
return
self.seen.append((event, args))
if event in self.raise_on_events:
raise self.exc_type("saw event " + event)
def test_simple_hook():
with TestHook() as hook:
sys.audit("test_event", 1, 2, 3)
assert hook.seen[0][0] == "test_event"
assert hook.seen[0][1] == (1, 2, 3)
def test_two_hooks():
l = []
def f(event, args):
l.append((1, event, args))
def g(event, args):
l.append((2, event, args))
sys.addaudithook(f)
sys.addaudithook(g)
try:
sys.audit("test")
assert l[-1] == (2, "test", ())
assert l[-2] == (1, "test", ())
finally:
if hasattr(__pypy__, '_testing_clear_audithooks'):
__pypy__._testing_clear_audithooks()
def test_block_add_hook():
# Raising an exception should prevent a new hook from being added,
# but will not propagate out.
with TestHook(raise_on_events="sys.addaudithook") as hook1:
with TestHook() as hook2:
sys.audit("test_event")
assert "test_event" in hook1.seen_events
assert hook2.seen_events == []
def test_id_hook():
with TestHook() as hook:
x = id(hook)
assert hook.seen[0] == ("builtins.id", (x, ))
@pytest.mark.pypy_only
def test_eval():
def ret1():
return 1
with TestHook() as hook:
res = eval(ret1.__code__)
assert res == 1
assert hook.seen == [("exec", (ret1.__code__, ))]
@pytest.mark.pypy_only
def test_exec():
def ret1():
return 1
with TestHook() as hook:
exec(ret1.__code__)
assert hook.seen == [("exec", (ret1.__code__, ))]
def test_sys_getframe():
with TestHook() as hook:
f = sys._getframe()
assert hook.seen == [("sys._getframe", (f,))]
def test_donttrace():
trace_events = []
audit_events = []
def trace(frame, evt, *args):
trace_events.append((evt, frame.f_code, frame.f_lineno))
return trace
def audit(evt, *args):
audit_events.append(evt)
sys.addaudithook(audit)
sys.settrace(trace)
try:
sys.audit("Überraschung!")
finally:
sys.settrace(None)
if hasattr(__pypy__, '_testing_clear_audithooks'):
__pypy__._testing_clear_audithooks()
print(trace_events)
assert len(trace_events) == 0
@pytest.mark.pypy_only
def test_cantrace():
trace_events = []
audit_events = []
def trace(frame, evt, *args):
trace_events.append((evt, frame.f_code, frame.f_lineno))
return trace
def audit(evt, *args):
audit_events.append(evt)
audit.__cantrace__ = 132
sys.addaudithook(audit)
sys.settrace(trace)
try:
sys.audit("Überraschung!")
finally:
sys.settrace(None)
if hasattr(__pypy__, '_testing_clear_audithooks'):
__pypy__._testing_clear_audithooks()
print(trace_events)
assert len(trace_events) == 3 # call, line, return
|