-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathPythonObjectInputStream.java
More file actions
53 lines (47 loc) · 1.73 KB
/
PythonObjectInputStream.java
File metadata and controls
53 lines (47 loc) · 1.73 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
// Copyright 2000 Finn Bock
package org.python.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import org.python.core.Py;
import org.python.core.PyObject;
import org.python.core.PyTuple;
import org.python.core.PyType;
import org.python.core.__builtin__;
public class PythonObjectInputStream extends ObjectInputStream {
public PythonObjectInputStream(InputStream istr) throws IOException {
super(istr);
}
protected Class<?> resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException {
String clsName = v.getName();
if (clsName.startsWith("org.python.proxies")) {
int idx = clsName.lastIndexOf('$');
if (idx > 19) {
clsName = clsName.substring(19, idx);
}
idx = clsName.indexOf('$');
if (idx >= 0) {
String mod = clsName.substring(0, idx);
clsName = clsName.substring(idx + 1);
PyObject module = importModule(mod);
PyType pycls = (PyType)module.__getattr__(clsName.intern());
return pycls.getProxyType();
}
}
try {
return super.resolveClass(v);
} catch (ClassNotFoundException exc) {
PyObject m = importModule(clsName);
Object cls = m.__tojava__(Class.class);
if (cls != null && cls != Py.NoConversion) {
return (Class<?>)cls;
}
throw exc;
}
}
private static PyObject importModule(String name) {
PyObject fromlist = new PyTuple(Py.newString("__doc__"));
return __builtin__.__import__(name, null, null, fromlist);
}
}