| 1 |
import pytest |
| 2 |
|
| 3 |
@pytest.fixture |
| 4 |
def hpy_abi(): |
| 5 |
return "debug" |
| 6 |
|
| 7 |
|
| 8 |
def test_reuse_context_from_global_variable(compiler, python_subprocess): |
| 9 |
mod = compiler.compile_module(""" |
| 10 |
#include <stdio.h> |
| 11 |
|
| 12 |
HPyContext *keep; |
| 13 |
|
| 14 |
HPyDef_METH(f, "f", HPyFunc_NOARGS) |
| 15 |
static HPy f_impl(HPyContext *ctx, HPy self) |
| 16 |
{ |
| 17 |
// We wrongly save the context to a global variable |
| 18 |
keep = ctx; |
| 19 |
return HPy_Dup(ctx, ctx->h_None); |
| 20 |
} |
| 21 |
|
| 22 |
HPyDef_METH(g, "g", HPyFunc_NOARGS) |
| 23 |
static HPy g_impl(HPyContext *ctx, HPy self) |
| 24 |
{ |
| 25 |
HPy t = HPy_Dup(ctx, ctx->h_True); |
| 26 |
// just checking if the correct context works |
| 27 |
if (!HPy_TypeCheck(ctx, t, ctx->h_BoolType)) { |
| 28 |
// if the correct context gives us bogus result, |
| 29 |
// this will make the test fail |
| 30 |
HPy_Close(ctx, t); |
| 31 |
return HPy_Dup(ctx, ctx->h_None); |
| 32 |
} |
| 33 |
HPy_Close(ctx, t); |
| 34 |
fprintf(stdout, "Heavy Marmelade\\n"); |
| 35 |
fflush(stdout); |
| 36 |
// Here we wrongly use "keep" instead of "ctx" |
| 37 |
return HPy_Dup(keep, ctx->h_None); |
| 38 |
} |
| 39 |
|
| 40 |
HPyDef_METH(bounce, "bounce", HPyFunc_O) |
| 41 |
static HPy bounce_impl(HPyContext *ctx, HPy self, HPy trampoline) |
| 42 |
{ |
| 43 |
fprintf(stdout, "Bouncing...\\n"); |
| 44 |
fflush(stdout); |
| 45 |
return HPy_CallTupleDict(ctx, trampoline, HPy_NULL, HPy_NULL); |
| 46 |
} |
| 47 |
|
| 48 |
HPyDef_METH(keep_and_bounce, "keep_and_bounce", HPyFunc_O) |
| 49 |
static HPy keep_and_bounce_impl(HPyContext *ctx, HPy self, HPy trampoline) |
| 50 |
{ |
| 51 |
fprintf(stdout, "Bouncing differently...\\n"); |
| 52 |
fflush(stdout); |
| 53 |
keep = ctx; |
| 54 |
return HPy_CallTupleDict(ctx, trampoline, HPy_NULL, HPy_NULL); |
| 55 |
} |
| 56 |
|
| 57 |
@EXPORT(f) |
| 58 |
@EXPORT(g) |
| 59 |
@EXPORT(bounce) |
| 60 |
@EXPORT(keep_and_bounce) |
| 61 |
@INIT |
| 62 |
""") |
| 63 |
|
| 64 |
code = "mod.f(); mod.g()" |
| 65 |
result = python_subprocess.run(mod, code) |
| 66 |
assert result.returncode != 0 |
| 67 |
assert b"Error: Wrong HPy Context!" in result.stderr |
| 68 |
assert result.stdout == b"Heavy Marmelade\n" |
| 69 |
|
| 70 |
code = "mod.f(); mod.bounce(lambda: mod.g())" |
| 71 |
result = python_subprocess.run(mod, code) |
| 72 |
assert result.returncode != 0 |
| 73 |
assert b"Error: Wrong HPy Context!" in result.stderr |
| 74 |
assert result.stdout == b"Bouncing...\nHeavy Marmelade\n" |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
code = "mod.f(); bounce_cnt = {};\n" \ |
| 79 |
"def trampoline():\n" \ |
| 80 |
" global bounce_cnt\n" \ |
| 81 |
" bounce_cnt -= 1\n" \ |
| 82 |
" return mod.bounce(trampoline) if bounce_cnt > 0 else mod.g()\n" \ |
| 83 |
"mod.bounce(trampoline)" |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
HPY_DEBUG_CTX_CACHE_SIZE = 16 |
| 92 |
for size in range(HPY_DEBUG_CTX_CACHE_SIZE-1, HPY_DEBUG_CTX_CACHE_SIZE+2): |
| 93 |
result = python_subprocess.run(mod, code.format(size)) |
| 94 |
assert result.stdout == (b"Bouncing...\n" * size) + b"Heavy Marmelade\n" |
| 95 |
if result.returncode != 0: |
| 96 |
assert b"Error: Wrong HPy Context!" in result.stderr |
| 97 |
|
| 98 |
code = 'mod.keep_and_bounce(lambda: mod.g())' |
| 99 |
result = python_subprocess.run(mod, code) |
| 100 |
assert result.returncode != 0 |
| 101 |
assert b"Error: Wrong HPy Context!" in result.stderr |
| 102 |
assert result.stdout == b"Bouncing differently...\n" + b"Heavy Marmelade\n" |