OILS / mycpp / examples / invalid_ctx_raise.py View on Github | oils.pub

51 lines, 23 significant
1#!/usr/bin/env python2
2"""
3invalid_ctx_raise.py
4"""
5from __future__ import print_function
6
7from typing import Any
8
9from mycpp import mylib
10
11
12class ShouldStartWithCtx(object):
13
14 def __exit__(self, type, value, traceback):
15 # type: (Any, Any, Any) -> None
16 pass
17
18
19class ctx_MaybePure(object):
20 """Regression for early return."""
21
22 def __init__(self):
23 # type: () -> None
24 self.member = 'bar'
25
26 def __enter__(self):
27 # type: () -> None
28 """no-op, but it has to exist to be used as context manager."""
29 pass
30
31 def __exit__(self, type, value, traceback):
32 # type: (Any, Any, Any) -> None
33
34 if self.member is not None:
35 # raise is not allowed
36 raise ValueError()
37
38
39def run_tests():
40 # type: () -> None
41
42 i = 0
43 for j in xrange(1000):
44 with ctx_MaybePure():
45 i += 1
46 mylib.MaybeCollect()
47 print(i)
48
49
50if __name__ == '__main__':
51 run_tests()