-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSysModule.java
More file actions
112 lines (96 loc) · 3.34 KB
/
Copy pathSysModule.java
File metadata and controls
112 lines (96 loc) · 3.34 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
package org.python.modules.sys;
import org.python.core.BuiltinDocs;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyFrame;
import org.python.core.PyObject;
import org.python.core.PyTuple;
import org.python.core.PyUnicode;
import org.python.expose.ExposedConst;
import org.python.expose.ExposedFunction;
import org.python.expose.ExposedModule;
import org.python.expose.ModuleInit;
import java.util.concurrent.Callable;
@ExposedModule(name = "sys")
public class SysModule {
@ExposedConst(name = "maxunicode")
public static final int MAXUNICODE = 0x10FFFF;
@ModuleInit
public static final void classDictInit(PyObject dict) {
dict.__setitem__("hash_info", new HashInfo());
}
@ExposedFunction(names = "exc_info", doc = BuiltinDocs.sys_exc_info_doc)
public static PyObject sys_exc_info() {
PyException exc = Py.getThreadState().exceptions.peek();
if (exc == null) {
return new PyTuple(Py.None, Py.None, Py.None);
}
PyObject tb = exc.traceback;
PyObject value = exc.value;
return new PyTuple(exc.type, value == null ? Py.None : value, tb == null ? Py.None : tb);
}
@ExposedFunction
public static PyObject getfilesystemencoding() {
return Py.getSystemState().getfilesystemencoding();
}
@ExposedFunction(defaults = "-1")
public static PyObject _getframe(int depth) {
PyFrame f = Py.getFrame();
while (depth > 0 && f != null) {
f = f.f_back;
--depth;
}
if (f == null) {
throw Py.ValueError("call stack is not deep enough");
}
return f;
}
@ExposedFunction
public static void settrace(PyObject tracefunc) {
Py.getSystemState().settrace(tracefunc);
}
@ExposedFunction
public static final PyObject gettrace() {
return Py.getSystemState().gettrace();
}
@ExposedFunction
public static PyObject registerCloser(final PyObject closer) {
Callable<Void> resourceCloser = new Callable<Void>() {
@Override
public Void call() throws Exception {
closer.__call__();
return null;
}
};
Py.getSystemState().registerCloser(resourceCloser);
return Py.None;
}
@ExposedFunction
public static PyObject unregisterCloser(Callable<Void> resourceCloser) {
return Py.newBoolean(Py.getSystemState().unregisterCloser(resourceCloser));
}
/**
* Exit a Python program with the given status.
*
* @param status the value to exit with
* @exception Py.SystemExit always throws this exception. When caught at top level the program
* will exit.
*/
@ExposedFunction(defaults = {"null"})
public static void exit(PyObject status) {
throw new PyException(Py.SystemExit, status);
}
// Java API
public static void setObject(String name, PyObject value) {
PyObject sysdict = Py.getSystemState().sysdict;
sysdict.__setitem__(name, value);
}
public static PyObject getObject(String name) {
PyObject sysdict = Py.getSystemState().sysdict;
return sysdict.__getitem__(name);
}
@ExposedFunction
public static PyObject intern(PyObject s) {
return new PyUnicode(s.toString().intern(), true);
}
}