| 1 |
""" |
| 2 |
NOTE: these tests are also meant to be run as PyPy "applevel" tests. |
| 3 |
|
| 4 |
This means that global imports will NOT be visible inside the test |
| 5 |
functions. In particular, you have to "import pytest" inside the test in order |
| 6 |
to be able to use e.g. pytest.raises (which on PyPy will be implemented by a |
| 7 |
"fake pytest module") |
| 8 |
""" |
| 9 |
import pytest |
| 10 |
from .support import HPyTest, DefaultExtensionTemplate |
| 11 |
|
| 12 |
class PairTemplate(DefaultExtensionTemplate): |
| 13 |
|
| 14 |
def DEFINE_PairObject(self): |
| 15 |
return """ |
| 16 |
typedef struct { |
| 17 |
HPyField a; |
| 18 |
HPyField b; |
| 19 |
} PairObject; |
| 20 |
|
| 21 |
HPyType_HELPERS(PairObject); |
| 22 |
""" |
| 23 |
|
| 24 |
def DEFINE_Pair_new(self): |
| 25 |
return """ |
| 26 |
HPyDef_SLOT(Pair_new, HPy_tp_new) |
| 27 |
static HPy Pair_new_impl(HPyContext *ctx, HPy cls, const HPy *args, |
| 28 |
HPy_ssize_t nargs, HPy kw) |
| 29 |
{ |
| 30 |
HPy a; |
| 31 |
HPy b; |
| 32 |
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &a, &b)) |
| 33 |
return HPy_NULL; |
| 34 |
PairObject *pair; |
| 35 |
HPy h_obj = HPy_New(ctx, cls, &pair); |
| 36 |
HPyField_Store(ctx, h_obj, &pair->a, a); |
| 37 |
HPyField_Store(ctx, h_obj, &pair->b, b); |
| 38 |
return h_obj; |
| 39 |
} |
| 40 |
""" |
| 41 |
|
| 42 |
def DEFINE_Pair_traverse(self): |
| 43 |
return """ |
| 44 |
HPyDef_SLOT(Pair_traverse, HPy_tp_traverse) |
| 45 |
static int Pair_traverse_impl(void *self, HPyFunc_visitproc visit, void *arg) |
| 46 |
{ |
| 47 |
PairObject *p = (PairObject *)self; |
| 48 |
HPy_VISIT(&p->a); |
| 49 |
HPy_VISIT(&p->b); |
| 50 |
return 0; |
| 51 |
} |
| 52 |
""" |
| 53 |
|
| 54 |
def DEFINE_Pair_set_a(self): |
| 55 |
return """ |
| 56 |
HPyDef_METH(Pair_set_a, "set_a", HPyFunc_O) |
| 57 |
static HPy Pair_set_a_impl(HPyContext *ctx, HPy self, HPy arg) |
| 58 |
{ |
| 59 |
PairObject *pair = PairObject_AsStruct(ctx, self); |
| 60 |
HPyField_Store(ctx, self, &pair->a, arg); |
| 61 |
return HPy_Dup(ctx, ctx->h_None); |
| 62 |
} |
| 63 |
""" |
| 64 |
|
| 65 |
def DEFINE_Pair_get_ab(self): |
| 66 |
return """ |
| 67 |
HPyDef_METH(Pair_get_a, "get_a", HPyFunc_NOARGS) |
| 68 |
static HPy Pair_get_a_impl(HPyContext *ctx, HPy self) |
| 69 |
{ |
| 70 |
PairObject *pair = PairObject_AsStruct(ctx, self); |
| 71 |
if (HPy_IsNull(pair->a)) |
| 72 |
return HPyUnicode_FromString(ctx, "<NULL>"); |
| 73 |
return HPyField_Load(ctx, self, pair->a); |
| 74 |
} |
| 75 |
|
| 76 |
HPyDef_METH(Pair_get_b, "get_b", HPyFunc_NOARGS) |
| 77 |
static HPy Pair_get_b_impl(HPyContext *ctx, HPy self) |
| 78 |
{ |
| 79 |
PairObject *pair = PairObject_AsStruct(ctx, self); |
| 80 |
if (HPy_IsNull(pair->b)) |
| 81 |
return HPyUnicode_FromString(ctx, "<NULL>"); |
| 82 |
return HPyField_Load(ctx, self, pair->b); |
| 83 |
} |
| 84 |
""" |
| 85 |
|
| 86 |
pair_type_flags = 'HPy_TPFLAGS_DEFAULT | HPy_TPFLAGS_HAVE_GC' |
| 87 |
def PAIR_TYPE_FLAGS(self, flags): |
| 88 |
self.pair_type_flags = flags |
| 89 |
|
| 90 |
def EXPORT_PAIR_TYPE(self, *defines): |
| 91 |
defines += ('NULL',) |
| 92 |
defines = ', '.join(defines) |
| 93 |
self.EXPORT_TYPE('"Pair"', "Pair_spec") |
| 94 |
return """ |
| 95 |
static HPyDef *Pair_defines[] = { %s }; |
| 96 |
static HPyType_Spec Pair_spec = { |
| 97 |
.name = "mytest.Pair", |
| 98 |
.basicsize = sizeof(PairObject), |
| 99 |
.flags = %s, |
| 100 |
.defines = Pair_defines |
| 101 |
}; |
| 102 |
""" % (defines, self.pair_type_flags) |
| 103 |
|
| 104 |
class TestHPyField(HPyTest): |
| 105 |
|
| 106 |
ExtensionTemplate = PairTemplate |
| 107 |
|
| 108 |
def test_gc_track(self): |
| 109 |
if not self.supports_refcounts(): |
| 110 |
import pytest |
| 111 |
pytest.skip("CPython only") |
| 112 |
import sys |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
import gc |
| 117 |
mod = self.make_module(""" |
| 118 |
@DEFINE_PairObject |
| 119 |
@DEFINE_Pair_new |
| 120 |
@DEFINE_Pair_traverse |
| 121 |
|
| 122 |
@EXPORT_PAIR_TYPE(&Pair_new, &Pair_traverse) |
| 123 |
@INIT |
| 124 |
""") |
| 125 |
p = mod.Pair("hello", "world") |
| 126 |
assert gc.is_tracked(p) |
| 127 |
|
| 128 |
def test_gc_track_no_gc_flag(self): |
| 129 |
if not self.supports_refcounts(): |
| 130 |
import pytest |
| 131 |
pytest.skip("CPython only") |
| 132 |
|
| 133 |
|
| 134 |
import gc |
| 135 |
mod = self.make_module(""" |
| 136 |
@DEFINE_PairObject |
| 137 |
@DEFINE_Pair_new |
| 138 |
@DEFINE_Pair_traverse |
| 139 |
|
| 140 |
@PAIR_TYPE_FLAGS(HPy_TPFLAGS_DEFAULT) |
| 141 |
@EXPORT_PAIR_TYPE(&Pair_new, &Pair_traverse) |
| 142 |
@INIT |
| 143 |
""") |
| 144 |
p = mod.Pair("hello", "world") |
| 145 |
assert not gc.is_tracked(p) |
| 146 |
|
| 147 |
def test_tp_traverse(self): |
| 148 |
import sys |
| 149 |
import gc |
| 150 |
mod = self.make_module(""" |
| 151 |
@DEFINE_PairObject |
| 152 |
@DEFINE_Pair_new |
| 153 |
@DEFINE_Pair_traverse |
| 154 |
|
| 155 |
@EXPORT_PAIR_TYPE(&Pair_new, &Pair_traverse) |
| 156 |
@INIT |
| 157 |
""") |
| 158 |
p = mod.Pair("hello", "world") |
| 159 |
|
| 160 |
referents = [x for x in gc.get_referents(p) if isinstance(x, str)] |
| 161 |
referents.sort() |
| 162 |
assert referents == ['hello', 'world'] |
| 163 |
|
| 164 |
def test_tp_traverse_sanity_check(self): |
| 165 |
import pytest |
| 166 |
with pytest.raises(ValueError): |
| 167 |
self.make_module(""" |
| 168 |
@DEFINE_PairObject |
| 169 |
@DEFINE_Pair_new |
| 170 |
|
| 171 |
// here we are defining a type with HPy_TPFLAGS_HAVE_GC but |
| 172 |
// without providing a tp_traverse |
| 173 |
@EXPORT_PAIR_TYPE(&Pair_new) |
| 174 |
@INIT |
| 175 |
""") |
| 176 |
|
| 177 |
def test_store_load(self): |
| 178 |
import sys |
| 179 |
mod = self.make_module(""" |
| 180 |
@DEFINE_PairObject |
| 181 |
@DEFINE_Pair_new |
| 182 |
@DEFINE_Pair_get_ab |
| 183 |
@DEFINE_Pair_traverse |
| 184 |
|
| 185 |
@PAIR_TYPE_FLAGS(HPy_TPFLAGS_DEFAULT) |
| 186 |
@EXPORT_PAIR_TYPE(&Pair_new, &Pair_traverse, &Pair_get_a, &Pair_get_b) |
| 187 |
@INIT |
| 188 |
""") |
| 189 |
p = mod.Pair("hello", "world") |
| 190 |
assert p.get_a() == 'hello' |
| 191 |
assert p.get_b() == 'world' |
| 192 |
|
| 193 |
|
| 194 |
if self.supports_refcounts(): |
| 195 |
a = object() |
| 196 |
a_refcnt = sys.getrefcount(a) |
| 197 |
p2 = mod.Pair(a, None) |
| 198 |
assert sys.getrefcount(a) == a_refcnt + 1 |
| 199 |
assert p2.get_a() is a |
| 200 |
assert sys.getrefcount(a) == a_refcnt + 1 |
| 201 |
|
| 202 |
def test_store_overwrite(self): |
| 203 |
import sys |
| 204 |
mod = self.make_module(""" |
| 205 |
#include <assert.h> |
| 206 |
@DEFINE_PairObject |
| 207 |
@DEFINE_Pair_new |
| 208 |
@DEFINE_Pair_get_ab |
| 209 |
@DEFINE_Pair_traverse |
| 210 |
@DEFINE_Pair_set_a |
| 211 |
|
| 212 |
HPyDef_METH(Pair_clear_a, "clear_a", HPyFunc_NOARGS) |
| 213 |
static HPy Pair_clear_a_impl(HPyContext *ctx, HPy self) |
| 214 |
{ |
| 215 |
PairObject *pair = PairObject_AsStruct(ctx, self); |
| 216 |
HPyField_Store(ctx, self, &pair->a, HPy_NULL); |
| 217 |
return HPy_Dup(ctx, ctx->h_None); |
| 218 |
} |
| 219 |
|
| 220 |
@PAIR_TYPE_FLAGS(HPy_TPFLAGS_DEFAULT) |
| 221 |
@EXPORT_PAIR_TYPE(&Pair_new, &Pair_traverse, &Pair_get_a, &Pair_get_b, &Pair_set_a, &Pair_clear_a) |
| 222 |
@INIT |
| 223 |
""") |
| 224 |
p = mod.Pair("hello", "world") |
| 225 |
assert p.get_a() == 'hello' |
| 226 |
assert p.get_b() == 'world' |
| 227 |
p.set_a('foo') |
| 228 |
assert p.get_a() == 'foo' |
| 229 |
p.clear_a() |
| 230 |
assert p.get_a() == '<NULL>' |
| 231 |
p.set_a('bar') |
| 232 |
assert p.get_a() == 'bar' |
| 233 |
|
| 234 |
|
| 235 |
if self.supports_refcounts(): |
| 236 |
a = object() |
| 237 |
a_refcnt = sys.getrefcount(a) |
| 238 |
p2 = mod.Pair(a, None) |
| 239 |
assert sys.getrefcount(a) == a_refcnt + 1 |
| 240 |
assert p2.get_a() is a |
| 241 |
p2.set_a('foo') |
| 242 |
assert sys.getrefcount(a) == a_refcnt |
| 243 |
p2.set_a(a) |
| 244 |
assert sys.getrefcount(a) == a_refcnt + 1 |
| 245 |
p2.clear_a() |
| 246 |
assert sys.getrefcount(a) == a_refcnt |
| 247 |
p2.clear_a() |
| 248 |
assert sys.getrefcount(a) == a_refcnt |
| 249 |
|
| 250 |
def test_automatic_tp_dealloc(self): |
| 251 |
import sys |
| 252 |
if not self.supports_refcounts(): |
| 253 |
import pytest |
| 254 |
pytest.skip("CPython only") |
| 255 |
|
| 256 |
import sys |
| 257 |
mod = self.make_module(""" |
| 258 |
@DEFINE_PairObject |
| 259 |
@DEFINE_Pair_new |
| 260 |
@DEFINE_Pair_traverse |
| 261 |
|
| 262 |
@EXPORT_PAIR_TYPE(&Pair_new, &Pair_traverse) |
| 263 |
@INIT |
| 264 |
""") |
| 265 |
a = object() |
| 266 |
b = object() |
| 267 |
p = mod.Pair(a, b) |
| 268 |
a_cnt = sys.getrefcount(a) |
| 269 |
b_cnt = sys.getrefcount(b) |
| 270 |
del p |
| 271 |
assert sys.getrefcount(a) == a_cnt - 1 |
| 272 |
assert sys.getrefcount(b) == b_cnt - 1 |
| 273 |
|
| 274 |
@pytest.mark.syncgc |
| 275 |
def test_automatic_tp_clear(self): |
| 276 |
if not self.supports_refcounts(): |
| 277 |
import pytest |
| 278 |
pytest.skip("CPython only") |
| 279 |
import sys |
| 280 |
|
| 281 |
import gc |
| 282 |
mod = self.make_module(""" |
| 283 |
@DEFINE_PairObject |
| 284 |
@DEFINE_Pair_new |
| 285 |
@DEFINE_Pair_traverse |
| 286 |
@DEFINE_Pair_set_a |
| 287 |
|
| 288 |
@EXPORT_PAIR_TYPE(&Pair_new, &Pair_traverse, &Pair_set_a) |
| 289 |
@INIT |
| 290 |
""") |
| 291 |
def count_pairs(): |
| 292 |
return len([obj for obj in gc.get_objects() if type(obj) is mod.Pair]) |
| 293 |
assert count_pairs() == 0 |
| 294 |
p1 = mod.Pair(None, 'hello') |
| 295 |
p2 = mod.Pair(None, 'world') |
| 296 |
p1.set_a(p2) |
| 297 |
p2.set_a(p1) |
| 298 |
assert count_pairs() == 2 |
| 299 |
|
| 300 |
try: |
| 301 |
gc.disable() |
| 302 |
del p1 |
| 303 |
del p2 |
| 304 |
assert count_pairs() == 2 |
| 305 |
finally: |
| 306 |
gc.enable() |
| 307 |
|
| 308 |
gc.collect() |
| 309 |
assert count_pairs() == 0 |
| 310 |
|
| 311 |
def test_tp_finalize(self): |
| 312 |
|
| 313 |
|
| 314 |
mod = self.make_module(""" |
| 315 |
#include <stdio.h> |
| 316 |
|
| 317 |
@DEFINE_PairObject |
| 318 |
@DEFINE_Pair_new |
| 319 |
@DEFINE_Pair_traverse |
| 320 |
@DEFINE_Pair_set_a |
| 321 |
|
| 322 |
static bool saw_expected_finalize_call; |
| 323 |
static bool unexpected_finalize_call; |
| 324 |
static bool test_finished; |
| 325 |
|
| 326 |
// During the test we check our assumptions with an assert to play |
| 327 |
// nicely with pytest, but if the finalizer gets called after the |
| 328 |
// test has finished, we have no choice but to abort to signal |
| 329 |
// the error |
| 330 |
static void on_unexpected_finalize_call(void); |
| 331 |
static void on_unexpected_finalize_call() { |
| 332 |
if (test_finished) |
| 333 |
abort(); |
| 334 |
else |
| 335 |
unexpected_finalize_call = true; |
| 336 |
} |
| 337 |
|
| 338 |
HPyDef_SLOT(Pair_finalize, HPy_tp_finalize) |
| 339 |
static void Pair_finalize_impl(HPyContext *ctx, HPy to_be_finalized) |
| 340 |
{ |
| 341 |
PairObject *pair = PairObject_AsStruct(ctx, to_be_finalized); |
| 342 |
|
| 343 |
// Check that we were called on the right object: 'to_be_finalized' |
| 344 |
HPy to_be_finalized_b = HPyField_Load(ctx, to_be_finalized, pair->b); |
| 345 |
if (!HPy_Is(ctx, to_be_finalized_b, ctx->h_True)) { |
| 346 |
HPy_Close(ctx, to_be_finalized_b); |
| 347 |
// This is OK to happen after the test was finished |
| 348 |
unexpected_finalize_call = true; |
| 349 |
return; |
| 350 |
} |
| 351 |
HPy_Close(ctx, to_be_finalized_b); |
| 352 |
|
| 353 |
// Check that we were not called twice |
| 354 |
if (saw_expected_finalize_call) { |
| 355 |
printf("tp_finalize called twice for 'to_be_finalized'\\\n"); |
| 356 |
on_unexpected_finalize_call(); |
| 357 |
return; |
| 358 |
} |
| 359 |
|
| 360 |
HPy owner = HPy_NULL, owner_a = HPy_NULL, owner_b = HPy_NULL; |
| 361 |
owner = HPyField_Load(ctx, to_be_finalized, pair->a); |
| 362 |
PairObject *owner_pair = PairObject_AsStruct(ctx, owner); |
| 363 |
|
| 364 |
// Check that 'to_be_finalized'->a really points to 'owner' |
| 365 |
owner_b = HPyField_Load(ctx, owner, owner_pair->b); |
| 366 |
if (!HPy_Is(ctx, owner_b, ctx->h_False)) { |
| 367 |
printf("to_be_finalized'->a != 'owner'\\\n"); |
| 368 |
on_unexpected_finalize_call(); |
| 369 |
goto owner_cleanup; |
| 370 |
} |
| 371 |
|
| 372 |
// Whatever we see in owner->a must not be 'to_be_finalized' |
| 373 |
// For CPython it should be NULL, because Pair_finalize should |
| 374 |
// be called immediately when the field is swapped to new value |
| 375 |
if (HPyField_IsNull(owner_pair->a)) { |
| 376 |
saw_expected_finalize_call = true; |
| 377 |
goto owner_cleanup; |
| 378 |
} |
| 379 |
|
| 380 |
// For GC based implementations, it can be already the 42 if |
| 381 |
// Pair_finalize gets called later |
| 382 |
owner_a = HPyField_Load(ctx, owner, owner_pair->a); |
| 383 |
if (HPyLong_AsLong(ctx, owner_a) == 42) { |
| 384 |
saw_expected_finalize_call = true; |
| 385 |
goto owner_cleanup; |
| 386 |
} |
| 387 |
|
| 388 |
HPyErr_Clear(ctx); // if the field was not a long at all |
| 389 |
printf("unexpected value of the field: %p\\\n", (void*) owner_a._i); |
| 390 |
on_unexpected_finalize_call(); |
| 391 |
owner_cleanup: |
| 392 |
HPy_Close(ctx, owner); |
| 393 |
HPy_Close(ctx, owner_a); |
| 394 |
HPy_Close(ctx, owner_b); |
| 395 |
} |
| 396 |
|
| 397 |
HPyDef_METH(check_finalize_calls, "check_finalize_calls", HPyFunc_NOARGS) |
| 398 |
static HPy check_finalize_calls_impl(HPyContext *ctx, HPy self) |
| 399 |
{ |
| 400 |
test_finished = true; |
| 401 |
if (!unexpected_finalize_call && saw_expected_finalize_call) |
| 402 |
return HPy_Dup(ctx, ctx->h_True); |
| 403 |
else |
| 404 |
return HPy_Dup(ctx, ctx->h_False); |
| 405 |
} |
| 406 |
|
| 407 |
@EXPORT(check_finalize_calls) |
| 408 |
@EXPORT_PAIR_TYPE(&Pair_new, &Pair_traverse, &Pair_finalize, &Pair_set_a) |
| 409 |
@INIT |
| 410 |
""") |
| 411 |
to_be_finalized = mod.Pair(None, True) |
| 412 |
owner = mod.Pair(to_be_finalized, False) |
| 413 |
to_be_finalized.set_a(owner) |
| 414 |
del to_be_finalized |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
owner.set_a(42) |
| 423 |
from gc import collect |
| 424 |
collect() |
| 425 |
assert mod.check_finalize_calls() |