-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext_mgr.py
47 lines (39 loc) · 1.18 KB
/
context_mgr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class GenContextManager(object):
def __init__(self, func_gen, *args, **kwargs):
self.gen = func_gen(*args, **kwargs)
def __enter__(self):
try:
v = next(self.gen)
return v
except StopIteration:
return
else:
raise RuntimeError("Generator threw exception on yield")
return
def __exit__(self, type, value, traceback):
if type is None:
try:
next(self.gen)
except StopIteration:
return
else:
raise RuntimeError("Generator failed to terminate")
else:
if value is None:
value = type()
#This is not enough...
#Few more exceptions to be handled
self.gen.throw(type, value, traceback)
pass
def file_op_ctx(file_op_gen_func):
def inner(*args, **kwargs):
return GenContextManager(file_op_gen_func, *args, **kwargs)
return inner
@file_op_ctx
def file_op(file_path):
fd = open(file_path, 'w')
yield fd
print ("Closing file")
fd.close()
with file_op("test.txt") as fd:
print ("huh??")