1
|
|
|
from __future__ import print_function |
2
|
|
|
|
3
|
|
|
import logging |
4
|
|
|
import re |
5
|
|
|
import sys |
6
|
|
|
import weakref |
7
|
|
|
|
8
|
|
|
import pytest |
9
|
|
|
|
10
|
|
|
import aspectlib |
11
|
|
|
import aspectlib.debug |
12
|
|
|
|
13
|
|
|
try: |
14
|
|
|
from StringIO import StringIO |
15
|
|
|
except ImportError: |
16
|
|
|
from io import StringIO |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
LOG_TEST_SIMPLE = r'''^some_meth\(1, 2, 3, a=4\) +<<< .*tests/test_aspectlib_debug.py:\d+:test_simple.* |
20
|
|
|
some_meth => \.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\. !"#\$%&\'\(\)\*\+,-\./0123456789:;<=>\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\\\]\^_`abcdefghijklmnopqrstuvwxyz\{\|\}~\.+ |
21
|
|
|
$''' |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def some_meth(*_args, **_kwargs): |
25
|
|
|
return ''.join(chr(i) for i in range(255)) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class MyStuff(object): |
29
|
|
|
def __init__(self, foo): |
30
|
|
|
self.foo = foo |
31
|
|
|
|
32
|
|
|
def bar(self): |
33
|
|
|
return 'foo' |
34
|
|
|
|
35
|
|
|
def stuff(self): |
36
|
|
|
return self.foo |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
class OldStuff: |
40
|
|
|
def __init__(self, foo): |
41
|
|
|
self.foo = foo |
42
|
|
|
|
43
|
|
|
def bar(self): |
44
|
|
|
return 'foo' |
45
|
|
|
|
46
|
|
|
def stuff(self): |
47
|
|
|
return self.foo |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
def test_simple(): |
51
|
|
|
buf = StringIO() |
52
|
|
|
with aspectlib.weave(some_meth, aspectlib.debug.log(print_to=buf, module=False, stacktrace=10)): |
53
|
|
|
some_meth(1, 2, 3, a=4) |
54
|
|
|
|
55
|
|
|
assert re.match(LOG_TEST_SIMPLE, buf.getvalue()) |
56
|
|
|
some_meth(1, 2, 3, a=4) |
57
|
|
|
assert re.match(LOG_TEST_SIMPLE, buf.getvalue()) |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
def test_fail_to_log(): |
61
|
|
|
@aspectlib.debug.log(print_to="crap") |
62
|
|
|
def foo(): |
63
|
|
|
pass |
64
|
|
|
foo() |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_logging_works(): |
68
|
|
|
buf = StringIO() |
69
|
|
|
ch = logging.StreamHandler(buf) |
70
|
|
|
ch.setLevel(logging.DEBUG) |
71
|
|
|
aspectlib.debug.logger.addHandler(ch) |
72
|
|
|
|
73
|
|
|
@aspectlib.debug.log |
74
|
|
|
def foo(): |
75
|
|
|
pass |
76
|
|
|
foo() |
77
|
|
|
assert re.match(r'foo\(\) +<<<.*\nfoo => None\n', buf.getvalue()) |
78
|
|
|
|
79
|
|
|
|
80
|
|
View Code Duplication |
def test_attributes(): |
|
|
|
|
81
|
|
|
buf = StringIO() |
82
|
|
|
with aspectlib.weave(MyStuff, aspectlib.debug.log( |
83
|
|
|
print_to=buf, |
84
|
|
|
stacktrace=10, |
85
|
|
|
attributes=('foo', 'bar()') |
86
|
|
|
), methods='(?!bar)(?!__.*__$)'): |
87
|
|
|
MyStuff('bar').stuff() |
88
|
|
|
print(buf.getvalue()) |
89
|
|
|
assert re.match(r"^\{test_aspectlib_debug.MyStuff foo='bar' bar='foo'\}.stuff\(\) +<<< .*tests/test_aspectlib_debug.py:\d+:test_attributes.*\n\{test_aspectlib_debug.MyStuff foo='bar' bar='foo'\}.stuff => bar\n$", buf.getvalue()) |
90
|
|
|
MyStuff('bar').stuff() |
91
|
|
|
assert re.match(r"^\{test_aspectlib_debug.MyStuff foo='bar' bar='foo'\}.stuff\(\) +<<< .*tests/test_aspectlib_debug.py:\d+:test_attributes.*\n\{test_aspectlib_debug.MyStuff foo='bar' bar='foo'\}.stuff => bar\n$", buf.getvalue()) |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
def test_no_stack(): |
95
|
|
|
buf = StringIO() |
96
|
|
|
with aspectlib.weave(MyStuff, aspectlib.debug.log( |
97
|
|
|
print_to=buf, |
98
|
|
|
stacktrace=None, |
99
|
|
|
attributes=('foo', 'bar()') |
100
|
|
|
), methods='(?!bar)(?!__.*__$)'): |
101
|
|
|
MyStuff('bar').stuff() |
102
|
|
|
print(buf.getvalue()) |
103
|
|
|
assert "{test_aspectlib_debug.MyStuff foo='bar' bar='foo'}.stuff()\n{test_aspectlib_debug.MyStuff foo='bar' bar='foo'}.stuff => bar\n" == buf.getvalue() |
104
|
|
|
|
105
|
|
|
|
106
|
|
View Code Duplication |
def test_attributes_old_style(): |
|
|
|
|
107
|
|
|
buf = StringIO() |
108
|
|
|
with aspectlib.weave(OldStuff, aspectlib.debug.log( |
109
|
|
|
print_to=buf, |
110
|
|
|
stacktrace=10, |
111
|
|
|
attributes=('foo', 'bar()') |
112
|
|
|
), methods='(?!bar)(?!__.*__$)'): |
113
|
|
|
OldStuff('bar').stuff() |
114
|
|
|
print(repr(buf.getvalue())) |
115
|
|
|
assert re.match(r"^\{test_aspectlib_debug.OldStuff foo='bar' bar='foo'\}.stuff\(\) +<<< .*tests/test_aspectlib_debug.py:\d+:test_attributes.*\n\{test_aspectlib_debug.OldStuff foo='bar' bar='foo'\}.stuff => bar\n$", buf.getvalue()) |
116
|
|
|
MyStuff('bar').stuff() |
117
|
|
|
assert re.match(r"^\{test_aspectlib_debug.OldStuff foo='bar' bar='foo'\}.stuff\(\) +<<< .*tests/test_aspectlib_debug.py:\d+:test_attributes.*\n\{test_aspectlib_debug.OldStuff foo='bar' bar='foo'\}.stuff => bar\n$", buf.getvalue()) |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
def test_no_stack_old_style(): |
121
|
|
|
buf = StringIO() |
122
|
|
|
with aspectlib.weave(OldStuff, aspectlib.debug.log( |
123
|
|
|
print_to=buf, |
124
|
|
|
stacktrace=None, |
125
|
|
|
attributes=('foo', 'bar()') |
126
|
|
|
), methods='(?!bar)(?!__.*__$)'): |
127
|
|
|
OldStuff('bar').stuff() |
128
|
|
|
print(buf.getvalue()) |
129
|
|
|
assert "{test_aspectlib_debug.OldStuff foo='bar' bar='foo'}.stuff()\n{test_aspectlib_debug.OldStuff foo='bar' bar='foo'}.stuff => bar\n" == buf.getvalue() |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
@pytest.mark.skipif(sys.version_info < (2, 7), reason="No weakref.WeakSet on Python<=2.6") |
133
|
|
|
def test_weakref(): |
134
|
|
|
with aspectlib.weave(MyStuff, aspectlib.debug.log): |
135
|
|
|
s = weakref.WeakSet() |
136
|
|
|
s.add(MyStuff.stuff) |
137
|
|
|
print(list(s)) |
138
|
|
|
print(list(s)) |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
@pytest.mark.skipif(sys.version_info < (2, 7), reason="No weakref.WeakSet on Python<=2.6") |
142
|
|
|
def test_weakref_oldstyle(): |
143
|
|
|
with aspectlib.weave(OldStuff, aspectlib.debug.log): |
144
|
|
|
s = weakref.WeakSet() |
145
|
|
|
s.add(MyStuff.stuff) |
146
|
|
|
print(list(s)) |
147
|
|
|
print(list(s)) |
148
|
|
|
|