File tree Expand file tree Collapse file tree 3 files changed +67
-0
lines changed
graalpython/lib-graalpython/modules/hpy/debug Expand file tree Collapse file tree 3 files changed +67
-0
lines changed Original file line number Diff line number Diff line change 1+ from .leakdetector import HPyDebugError , HPyLeakError , LeakDetector
Original file line number Diff line number Diff line change 1+ from hpy .universal import _debug
2+
3+ class HPyDebugError (Exception ):
4+ pass
5+
6+ class HPyLeakError (HPyDebugError ):
7+ def __init__ (self , leaks ):
8+ super ().__init__ ()
9+ self .leaks = leaks
10+
11+ def __str__ (self ):
12+ lines = []
13+ n = len (self .leaks )
14+ s = 's' if n != 1 else ''
15+ lines .append (f'{ n } unclosed handle{ s } :' )
16+ for dh in self .leaks :
17+ lines .append (' %r' % dh )
18+ return '\n ' .join (lines )
19+
20+
21+ class LeakDetector :
22+
23+ def __init__ (self ):
24+ self .generation = None
25+
26+ def start (self ):
27+ if self .generation is not None :
28+ raise ValueError ('LeakDetector already started' )
29+ self .generation = _debug .new_generation ()
30+
31+ def stop (self ):
32+ if self .generation is None :
33+ raise ValueError ('LeakDetector not started yet' )
34+ leaks = _debug .get_open_handles (self .generation )
35+ if leaks :
36+ raise HPyLeakError (leaks )
37+
38+ def __enter__ (self ):
39+ self .start ()
40+ return self
41+
42+ def __exit__ (self , etype , evalue , tb ):
43+ self .stop ()
Original file line number Diff line number Diff line change 1+ # hpy.debug / pytest integration
2+
3+ import pytest
4+ from .leakdetector import LeakDetector
5+
6+ # For now "hpy_debug" just does leak detection, but in the future it might
7+ # grows extra features: that's why it's called generically "hpy_debug" instead
8+ # of "detect_leaks".
9+
10+ # NOTE: the fixture itself is currently untested :(. It turns out that testing
11+ # that the fixture raises during the teardown is complicated and probably
12+ # requires to write a full-fledged plugin. We might want to turn this into a
13+ # real plugin in the future, but for now I think this is enough.
14+
15+ @pytest .fixture
16+ def hpy_debug (request ):
17+ """
18+ pytest fixture which makes it possible to control hpy.debug from within a test.
19+
20+ In particular, it automatically check that the test doesn't leak.
21+ """
22+ with LeakDetector () as ld :
23+ yield ld
You can’t perform that action at this time.
0 commit comments