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

44 lines, 20 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 ctx_MaybePure(object):
13 """Regression for early return."""
14
15 def __init__(self):
16 # type: () -> None
17 self.member = 'bar'
18
19 def __enter__(self):
20 # type: () -> None
21 """no-op, but it has to exist to be used as context manager."""
22 pass
23
24 def __exit__(self, type, value, traceback):
25 # type: (Any, Any, Any) -> None
26
27 if self.member is not None:
28 # raise is not allowed
29 raise ValueError()
30
31
32def run_tests():
33 # type: () -> None
34
35 i = 0
36 for j in xrange(1000):
37 with ctx_MaybePure():
38 i += 1
39 mylib.MaybeCollect()
40 print(i)
41
42
43if __name__ == '__main__':
44 run_tests()