-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathAsyncContextGuard.java
More file actions
47 lines (42 loc) · 1.39 KB
/
Copy pathAsyncContextGuard.java
File metadata and controls
47 lines (42 loc) · 1.39 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
package org.python.core;
/**
* Created by isaiah on 5/18/16.
*/
public class AsyncContextGuard implements AsyncContextManager {
private final PyObject __aenter__method;
private final PyObject __aexit__method;
private PyException exception;
public AsyncContextGuard(PyObject manager) {
__aexit__method = manager.__getattr__("__aexit__");
__aenter__method = manager.__getattr__("__aenter__");
}
public static AsyncContextManager getManager(PyObject manager) {
if (manager instanceof AsyncContextManager) {
return (AsyncContextManager) manager;
}
return new AsyncContextGuard(manager);
}
@Override
public PyObject __aenter__(ThreadState ts) {
return __aenter__method.__call__(ts);
}
@Override
public PyObject __aexit__(ThreadState ts, PyException exception) {
this.exception = 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 __aexit__method.__call__(ts, type,
value == null ? Py.None : value,
traceback == null ? Py.None : traceback);
}
@Override
public PyException exception() {
return exception;
}
}