Debian Codegraph @

extra_tests/ctypes_tests/support.py

raw symbols SCIP
1 import py
2 import sys
3 import ctypes
4
5 try:
6 import _rawffi
7 except ImportError:
8 _rawffi = None
9
10 class WhiteBoxTests:
11
12 def setup_class(cls):
13 if _rawffi:
14 py.test.skip("white-box tests for pypy _rawffi based ctypes impl")
15
16 def del_funcptr_refs_maybe(obj, attrname):
17 dll = getattr(obj, attrname, None)
18 if not dll:
19 return
20 _FuncPtr = dll._FuncPtr
21 for name in dir(dll):
22 obj = getattr(dll, name, None)
23 if isinstance(obj, _FuncPtr):
24 delattr(dll, name)
25
26 class BaseCTypesTestChecker:
27 def setup_class(cls):
28 if _rawffi:
29 import gc
30 for _ in range(4):
31 gc.collect()
32 try:
33 cls.old_num = _rawffi._num_of_allocated_objects()
34 except RuntimeError:
35 pass
36
37 def teardown_class(cls):
38 if not hasattr(sys, 'pypy_translation_info'):
39 return
40 if sys.pypy_translation_info['translation.gc'] == 'boehm':
41 return # it seems that boehm has problems with __del__, so not
42 # everything is freed
43 #
44 mod = sys.modules[cls.__module__]
45 del_funcptr_refs_maybe(mod, 'dll')
46 del_funcptr_refs_maybe(mod, 'dll2')
47 del_funcptr_refs_maybe(mod, 'lib')
48 del_funcptr_refs_maybe(mod, 'testdll')
49 del_funcptr_refs_maybe(mod, 'ctdll')
50 del_funcptr_refs_maybe(cls, '_dll')
51 #
52 if hasattr(cls, 'old_num'):
53 import gc
54 for _ in range(4):
55 gc.collect()
56 # there is one reference coming from the byref() above
57 assert _rawffi._num_of_allocated_objects() == cls.old_num