| 1 |
import pytest |
| 2 |
from hpy.debug import set_handle_stack_trace_limit, disable_handle_stack_traces |
| 3 |
|
| 4 |
@pytest.fixture |
| 5 |
def hpy_abi(): |
| 6 |
return "debug" |
| 7 |
|
| 8 |
|
| 9 |
class AllocationTraceEnabler: |
| 10 |
def __enter__(self): |
| 11 |
set_handle_stack_trace_limit(32) |
| 12 |
|
| 13 |
def __exit__(self, exc_type, exc_val, exc_tb): |
| 14 |
disable_handle_stack_traces() |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture(params=["with stacktrace", "no stacktrace"]) |
| 18 |
def with_alloc_trace(request): |
| 19 |
if request.param == "with stacktrace": |
| 20 |
with AllocationTraceEnabler(): |
| 21 |
yield |
| 22 |
else: |
| 23 |
yield |
| 24 |
|
| 25 |
|
| 26 |
def make_leak_module(compiler): |
| 27 |
|
| 28 |
return compiler.make_module(""" |
| 29 |
HPyDef_METH(leak, "leak", HPyFunc_O) |
| 30 |
static HPy leak_impl(HPyContext *ctx, HPy self, HPy arg) |
| 31 |
{ |
| 32 |
HPy_Dup(ctx, arg); // leak! |
| 33 |
return HPy_Dup(ctx, ctx->h_None); |
| 34 |
} |
| 35 |
@EXPORT(leak) |
| 36 |
@INIT |
| 37 |
""") |
| 38 |
|
| 39 |
|
| 40 |
def test_debug_ctx_name(compiler): |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
mod = compiler.make_module(""" |
| 47 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 48 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 49 |
{ |
| 50 |
return HPyUnicode_FromString(ctx, ctx->name); |
| 51 |
} |
| 52 |
|
| 53 |
@EXPORT(f) |
| 54 |
@INIT |
| 55 |
""") |
| 56 |
ctx_name = mod.f() |
| 57 |
assert ctx_name.startswith('HPy Debug Mode ABI') |
| 58 |
|
| 59 |
def test_get_open_handles(compiler): |
| 60 |
from hpy.universal import _debug |
| 61 |
mod = make_leak_module(compiler) |
| 62 |
gen1 = _debug.new_generation() |
| 63 |
mod.leak('hello') |
| 64 |
mod.leak('world') |
| 65 |
gen2 = _debug.new_generation() |
| 66 |
mod.leak('a younger leak') |
| 67 |
leaks1 = _debug.get_open_handles(gen1) |
| 68 |
leaks2 = _debug.get_open_handles(gen2) |
| 69 |
leaks1 = [dh.obj for dh in leaks1] |
| 70 |
leaks2 = [dh.obj for dh in leaks2] |
| 71 |
assert leaks1 == ['hello', 'world', 'a younger leak'] |
| 72 |
assert leaks2 == ['a younger leak'] |
| 73 |
|
| 74 |
def test_leak_from_method(compiler): |
| 75 |
from hpy.universal import _debug |
| 76 |
mod = compiler.make_module(""" |
| 77 |
HPyDef_METH(Dummy_leak, "leak", HPyFunc_O) |
| 78 |
static HPy Dummy_leak_impl(HPyContext *ctx, HPy self, HPy arg) { |
| 79 |
HPy_Dup(ctx, arg); // leak! |
| 80 |
return HPy_Dup(ctx, ctx->h_None); |
| 81 |
} |
| 82 |
static HPyDef *Dummy_defines[] = { |
| 83 |
&Dummy_leak, |
| 84 |
NULL |
| 85 |
}; |
| 86 |
static HPyType_Spec Dummy_spec = { |
| 87 |
.name = "mytest.Dummy", |
| 88 |
.defines = Dummy_defines, |
| 89 |
}; |
| 90 |
@EXPORT_TYPE("Dummy", Dummy_spec) |
| 91 |
@INIT |
| 92 |
""") |
| 93 |
gen = _debug.new_generation() |
| 94 |
obj = mod.Dummy() |
| 95 |
obj.leak("a") |
| 96 |
leaks = [dh.obj for dh in _debug.get_open_handles(gen)] |
| 97 |
assert leaks == ["a"] |
| 98 |
|
| 99 |
def test_DebugHandle_id(compiler, with_alloc_trace): |
| 100 |
from hpy.universal import _debug |
| 101 |
mod = make_leak_module(compiler) |
| 102 |
gen = _debug.new_generation() |
| 103 |
mod.leak('a') |
| 104 |
mod.leak('b') |
| 105 |
a1, b1 = _debug.get_open_handles(gen) |
| 106 |
a2, b2 = _debug.get_open_handles(gen) |
| 107 |
assert a1.obj == a2.obj == 'a' |
| 108 |
assert b1.obj == b2.obj == 'b' |
| 109 |
|
| 110 |
assert a1 is not a2 |
| 111 |
assert b1 is not b2 |
| 112 |
|
| 113 |
assert a1.id == a2.id |
| 114 |
assert b1.id == b2.id |
| 115 |
assert a1.id != b1.id |
| 116 |
|
| 117 |
def test_DebugHandle_compare(compiler): |
| 118 |
import pytest |
| 119 |
from hpy.universal import _debug |
| 120 |
mod = make_leak_module(compiler) |
| 121 |
gen = _debug.new_generation() |
| 122 |
mod.leak('a') |
| 123 |
mod.leak('a') |
| 124 |
a2, a1 = _debug.get_open_handles(gen) |
| 125 |
assert a1 != a2 |
| 126 |
|
| 127 |
a2_new, a1_new = _debug.get_open_handles(gen) |
| 128 |
assert a1 is not a1_new |
| 129 |
assert a2 is not a2_new |
| 130 |
assert a1 == a1_new |
| 131 |
assert a2 == a2_new |
| 132 |
|
| 133 |
with pytest.raises(TypeError): |
| 134 |
a1 < a2 |
| 135 |
with pytest.raises(TypeError): |
| 136 |
a1 <= a2 |
| 137 |
with pytest.raises(TypeError): |
| 138 |
a1 > a2 |
| 139 |
with pytest.raises(TypeError): |
| 140 |
a1 >= a2 |
| 141 |
|
| 142 |
assert not a1 == 'hello' |
| 143 |
assert a1 != 'hello' |
| 144 |
with pytest.raises(TypeError): |
| 145 |
a1 < 'hello' |
| 146 |
|
| 147 |
def test_DebugHandle_repr(compiler, with_alloc_trace): |
| 148 |
from hpy.universal import _debug |
| 149 |
mod = make_leak_module(compiler) |
| 150 |
gen = _debug.new_generation() |
| 151 |
mod.leak('hello') |
| 152 |
h_hello, = _debug.get_open_handles(gen) |
| 153 |
assert repr(h_hello).startswith("<DebugHandle 0x%x for 'hello'>" % h_hello.id) |
| 154 |
|
| 155 |
def test_LeakDetector(compiler): |
| 156 |
import pytest |
| 157 |
from hpy.debug import LeakDetector, HPyLeakError |
| 158 |
mod = make_leak_module(compiler) |
| 159 |
ld = LeakDetector() |
| 160 |
ld.start() |
| 161 |
mod.leak('hello') |
| 162 |
with pytest.raises(HPyLeakError) as exc: |
| 163 |
ld.stop() |
| 164 |
assert str(exc.value).startswith('1 unclosed handle:') |
| 165 |
|
| 166 |
with pytest.raises(HPyLeakError) as exc: |
| 167 |
with LeakDetector(): |
| 168 |
mod.leak('foo') |
| 169 |
mod.leak('bar') |
| 170 |
mod.leak('baz') |
| 171 |
msg = str(exc.value) |
| 172 |
assert msg.startswith('3 unclosed handles:') |
| 173 |
assert 'foo' in msg |
| 174 |
assert 'bar' in msg |
| 175 |
assert 'baz' in msg |
| 176 |
assert 'hello' not in msg |
| 177 |
assert 'world' not in msg |
| 178 |
|
| 179 |
def test_closed_handles(compiler, with_alloc_trace): |
| 180 |
from hpy.universal import _debug |
| 181 |
mod = make_leak_module(compiler) |
| 182 |
gen = _debug.new_generation() |
| 183 |
mod.leak('hello') |
| 184 |
h_hello, = _debug.get_open_handles(gen) |
| 185 |
assert not h_hello.is_closed |
| 186 |
h_hello._force_close() |
| 187 |
assert h_hello.is_closed |
| 188 |
assert _debug.get_open_handles(gen) == [] |
| 189 |
assert h_hello in _debug.get_closed_handles() |
| 190 |
assert repr(h_hello).startswith("<DebugHandle 0x%x CLOSED>" % h_hello.id) |
| 191 |
|
| 192 |
def test_closed_handles_queue_max_size(compiler): |
| 193 |
from hpy.universal import _debug |
| 194 |
mod = compiler.make_module(""" |
| 195 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 196 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 197 |
{ |
| 198 |
return HPy_Dup(ctx, ctx->h_None); |
| 199 |
} |
| 200 |
@EXPORT(f) |
| 201 |
@INIT |
| 202 |
""") |
| 203 |
old_size = _debug.get_closed_handles_queue_max_size() |
| 204 |
try: |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
n1 = len(_debug.get_closed_handles()) |
| 209 |
_debug.set_closed_handles_queue_max_size(n1+7) |
| 210 |
assert _debug.get_closed_handles_queue_max_size() == n1+7 |
| 211 |
|
| 212 |
mod.f('aaa') |
| 213 |
n2 = len(_debug.get_closed_handles()) |
| 214 |
assert n2 == n1+3 |
| 215 |
|
| 216 |
mod.f('bbb') |
| 217 |
n3 = len(_debug.get_closed_handles()) |
| 218 |
assert n3 == n2+3 |
| 219 |
|
| 220 |
mod.f('ccc') |
| 221 |
n4 = len(_debug.get_closed_handles()) |
| 222 |
assert n4 == n1+7 |
| 223 |
|
| 224 |
|
| 225 |
mod.f('ddd') |
| 226 |
n5 = len(_debug.get_closed_handles()) |
| 227 |
assert n5 == n1+7 |
| 228 |
finally: |
| 229 |
_debug.set_closed_handles_queue_max_size(old_size) |
| 230 |
|
| 231 |
def test_reuse_closed_handles(compiler): |
| 232 |
from hpy.universal import _debug |
| 233 |
mod = compiler.make_module(""" |
| 234 |
HPyDef_METH(f, "f", HPyFunc_O) |
| 235 |
static HPy f_impl(HPyContext *ctx, HPy self, HPy arg) |
| 236 |
{ |
| 237 |
return HPy_Dup(ctx, ctx->h_None); |
| 238 |
} |
| 239 |
HPyDef_METH(leak, "leak", HPyFunc_O) |
| 240 |
static HPy leak_impl(HPyContext *ctx, HPy self, HPy arg) |
| 241 |
{ |
| 242 |
HPy_Dup(ctx, arg); // leak! |
| 243 |
return HPy_Dup(ctx, ctx->h_None); |
| 244 |
} |
| 245 |
@EXPORT(f) |
| 246 |
@EXPORT(leak) |
| 247 |
@INIT |
| 248 |
""") |
| 249 |
|
| 250 |
old_size = _debug.get_closed_handles_queue_max_size() |
| 251 |
try: |
| 252 |
gen = _debug.new_generation() |
| 253 |
|
| 254 |
mod.f('hello') |
| 255 |
mod.f('world') |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
_debug.set_closed_handles_queue_max_size(1) |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
closed_handles = _debug.get_closed_handles() |
| 267 |
mod.leak('foo') |
| 268 |
assert not closed_handles[2].is_closed |
| 269 |
assert closed_handles[2].obj == 'foo' |
| 270 |
|
| 271 |
closed_handles = _debug.get_closed_handles() |
| 272 |
mod.leak('bar') |
| 273 |
assert not closed_handles[2].is_closed |
| 274 |
assert closed_handles[2].obj == 'bar' |
| 275 |
|
| 276 |
leaks = _debug.get_open_handles(gen) |
| 277 |
for h in leaks: |
| 278 |
h._force_close() |
| 279 |
finally: |
| 280 |
_debug.set_closed_handles_queue_max_size(old_size) |