-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathContextGuard.java
More file actions
183 lines (162 loc) · 5.97 KB
/
ContextGuard.java
File metadata and controls
183 lines (162 loc) · 5.97 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package org.python.core;
/**
* Straightens the call path for some common cases
*/
public class ContextGuard implements ContextManager {
private final PyObject __enter__method;
private final PyObject __exit__method;
private ContextGuard(PyObject manager) {
__exit__method = manager.__getattr__("__exit__");
__enter__method = manager.__getattr__("__enter__");
}
public PyObject __enter__(ThreadState ts) {
return __enter__method.__call__(ts);
}
public boolean __exit__(ThreadState ts, PyException exception) {
final PyObject type, value, traceback;
if (exception != null) {
type = exception.type;
value = exception.value;
traceback = exception.traceback;
} else {
type = value = traceback = Py.None;
}
return __exit__method.__call__(ts, type,
value == null ? Py.None : value,
traceback == null ? Py.None : traceback).__nonzero__();
}
public static ContextManager getManager(PyObject manager) {
if (manager instanceof ContextManager) {
return (ContextManager) manager;
} else {
return new ContextGuard(manager);
}
}
// XXX - tentative support for generators in conjunction w/ contextlib.contextmanager
/* Sample usage:
*
* from org.python.core.ContextGuard import makeManager as contextmanager
* @contextmanager
* def my_manager():
* print "setup"
* try:
* yield
* finally:
* print "done"
*/
public static PyObject makeManager(PyObject object) {
if (object instanceof PyFunction) {
PyFunction function = (PyFunction) object;
PyCode code = function.__code__;
if (code instanceof PyBaseCode) {
PyBaseCode pyCode = (PyBaseCode) code;
if (pyCode.co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
return new PyFunction(function.__globals__,
function.__defaults__,
new ContextCode(pyCode),
function.__doc__,
(function.__closure__ == null) ? null :
((PyTuple)function.__closure__).getArray());
}
}
}
throw Py.TypeError("Argument must be a generator function.");
}
private static class ContextCode extends PyBaseCode implements Traverseproc {
private final PyBaseCode code;
ContextCode(PyBaseCode code) {
this.co_name = code.co_name;
this.code = code;
this.co_argcount = code.co_argcount;
this.nargs = code.nargs;
this.co_firstlineno = code.co_firstlineno;
this.co_varnames = code.co_varnames;
this.co_cellvars = code.co_cellvars;
this.jy_npurecell = code.jy_npurecell;
this.co_freevars = code.co_freevars;
this.co_filename = code.co_filename;
this.co_nlocals = code.co_nlocals;
this.varargs = code.varargs;
this.varkwargs = code.varkwargs;
for (CodeFlag flag : CodeFlag.values()) {
if (code.co_flags.isFlagSet(flag) && flag != CodeFlag.CO_GENERATOR) {
this.co_flags.setFlag(flag);
}
}
}
@SuppressWarnings("serial")
@Override
protected PyObject interpret(PyFrame frame, ThreadState ts) {
frame.f_back = null;
return new GeneratorContextManager(frame) {
@Override
PyObject body(ThreadState ts) {
return code.interpret(frame, ts);
}
};
}
@SuppressWarnings("serial")
@Override
public PyObject call(ThreadState ts, PyFrame frame, final PyObject closure) {
frame.f_back = null;
return new GeneratorContextManager(frame) {
@Override
PyObject body(ThreadState ts) {
return code.call(ts, frame, closure);
}
};
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
return code != null ? visit.visit(code, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && ob == code;
}
}
@SuppressWarnings("serial")
private static abstract class GeneratorContextManager extends PyObject implements ContextManager, Traverseproc {
final PyFrame frame;
public GeneratorContextManager(PyFrame frame) {
this.frame = frame;
}
public PyObject __enter__(ThreadState ts) {
PyObject res = body(ts);
if (frame.f_lasti == -1) {
throw Py.RuntimeError("generator didn't yield");
}
return res;
}
public boolean __exit__(ThreadState ts, PyException exception) {
if (exception != null) {
frame.setGeneratorInput(exception);
}
final PyObject res;
try {
res = body(ts);
} catch(PyException e) {
if (e.equals(exception)) {
return false;
} else {
throw e;
}
}
if (frame.f_lasti != -1) {
throw Py.RuntimeError("generator didn't stop");
}
return res.__nonzero__();
}
abstract PyObject body(ThreadState ts);
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
return frame != null ? visit.visit(frame, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && ob == frame;
}
}
}