-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathClasspathPyImporter.java
More file actions
266 lines (237 loc) · 8.57 KB
/
Copy pathClasspathPyImporter.java
File metadata and controls
266 lines (237 loc) · 8.57 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* Copyright (c) Jython Developers */
package org.python.core;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.python.core.util.FileUtil;
import org.python.core.util.StringUtil;
import org.python.core.util.importer;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import org.python.util.Generic;
// TODO: This could be replaced by the frozen module machinery, implement _imp.is_frozen / exec_frozen properly
// NOTE: this class should be kept as a fallback to bootstrap the import machinery, before we find a way to freeze
// the importlib/_bootstrap(_external).py without a working import mechanism
@Untraversable
@ExposedType(name="ClasspathPyImporter")
public class ClasspathPyImporter extends importer {
public static final String PYCLASSPATH_PREFIX = "__pyclasspath__/";
public static final PyType TYPE = PyType.fromClass(ClasspathPyImporter.class);
public ClasspathPyImporter(PyType subType) {
super(subType);
}
public ClasspathPyImporter() {
super(TYPE);
}
@ExposedNew
@ExposedMethod
final void ClasspathPyImporter___init__(PyObject[] args, String[] kwds) {
ArgParser ap = new ArgParser("__init__", args, kwds, new String[] {"path"});
String path = ap.getString(0);
if (!path.endsWith(File.separator)) {
path += File.separator;
}
this.path = path;
}
/**
* Return the contents of the jarred file at the specified path
* as bytes.
*
* @param path a String path name within the archive
* @return a String of data in binary mode (no CRLF)
*/
@Override
public String get_data(String path) {
return ClasspathPyImporter_get_data(path);
}
@ExposedMethod
final String ClasspathPyImporter_get_data(String path) {
// Strip any leading occurrence of the hook string
// int len = PYCLASSPATH_PREFIX.length();
// if (len < path.length() && path.startsWith(PYCLASSPATH_PREFIX)) {
// path = path.substring(len);
// }
// Bundle wraps the stream together with a close operation
try (Bundle bundle = makeBundle(makeEntry(path))) {
byte[] data = FileUtil.readBytes(bundle.inputStream);
return StringUtil.fromBytes(data);
} catch (IOException ioe) {
throw Py.IOError(ioe);
}
}
/**
* Return the source code for the module as a string (using
* newline characters for line endings)
*
* @param fullname the fully qualified name of the module
* @return a String of the module's source code or null
*/
public String get_source(String fullname) {
return ClasspathPyImporter_get_source(fullname);
}
@ExposedMethod
final String ClasspathPyImporter_get_source(String fullname) {
ModuleInfo moduleInfo = getModuleInfo(fullname);
if (moduleInfo.notFound()) {
throw Py.ImportError(String.format("can't find module '%s'", fullname), fullname);
}
try (InputStream is = new FileInputStream(new File(moduleInfo.getSourcePath()))) {
byte[] data = FileUtil.readBytes(is);
return StringUtil.fromBytes(data);
} catch (IOException e) {
throw Py.ImportError(String.format("source of %s not found", fullname));
}
}
/**
* Find the module for the fully qualified name.
*
* @param fullname the fully qualified name of the module
* @param path if not installed on the meta-path None or a module path
* @return a loader instance if this importer can load the module, None
* otherwise
*/
@ExposedMethod(defaults = "null")
final PyObject ClasspathPyImporter_find_module(String fullname, String path) {
return importer_find_module(fullname, path);
}
@ExposedMethod
final PyObject ClasspathPyImporter_find_spec(PyObject[] args, String[] keywords) {
ArgParser ap = new ArgParser("find_spec", args, keywords, "fullname", "path", "target");
String fullname = ap.getString(0);
PyObject path = ap.getPyObject(1, Py.None);
PyObject target = ap.getPyObject(2, Py.None);
PyObject spec = importer_find_spec(fullname);
if (spec == null) {
return Py.None;
}
spec.__setattr__("has_location", Py.True);
return spec;
}
/**
* Determine whether a module is a package.
*
* @param fullname the fully qualified name of the module
* @return whether the module is a package
*/
@ExposedMethod
final boolean ClasspathPyImporter_is_package(String fullname) {
return importer_is_package(fullname);
}
/**
* Return the code object associated with the module.
*
* @param fullname the fully qualified name of the module
* @return the module's PyCode object or None
*/
@ExposedMethod
final PyObject ClasspathPyImporter_get_code(String fullname) {
ModuleCodeData moduleCodeData = getModuleCode(fullname);
if (moduleCodeData != null) {
return moduleCodeData.code;
}
return Py.None;
}
/**
* Load a module for the fully qualified name.
*
* @param fullname the fully qualified name of the module
* @return a loaded PyModule
*/
@ExposedMethod
final PyObject ClasspathPyImporter_load_module(String fullname) {
return importer_load_module(fullname);
}
@ExposedMethod
final PyObject ClasspathPyImporter_create_module(PyObject spec) {
String fullname = spec.__getattr__("name").asString();
// the module *must* be in sys.modules before the loader executes the module code; the
// module code may (directly or indirectly) import itself
PyModule mod = imp.addModule(fullname);
mod.__setattr__("__loader__", this);
return mod;
}
@ExposedMethod
final PyObject ClasspathPyImporter_exec_module(PyObject module) {
PyModule mod = (PyModule) module;
String fullname = mod.__findattr__("__name__").asString();
ModuleCodeData moduleCodeData = getModuleCode(fullname);
return imp.createFromCode(fullname, moduleCodeData.code);
}
@Override
protected long getSourceMtime(String path) {
// Can't determine this easily
return -1;
}
@Override
protected Bundle makeBundle(String entry) {
InputStream is = entries.remove(entry);
return new Bundle(is) {
@Override
public void close() {
try {
inputStream.close();
} catch (IOException e) {
throw Py.JavaError(e);
}
}
};
}
@Override
protected String makeEntry(String filename) {
if (entries.containsKey(filename)) {
return filename;
}
InputStream is;
File input = new File(filename);
try {
is = new FileInputStream(input);
} catch (FileNotFoundException e) {
return null;
}
// ClassLoader sysClassLoader = Py.getSystemState().getClassLoader();
// if (sysClassLoader != null) {
// is = tryClassLoader(filename, sysClassLoader, "sys");
// } else {
// is = tryClassLoader(filename, imp.getParentClassLoader(), "parent");
// }
// if (is == null) {
// return null;
// }
entries.put(filename, is);
return filename;
}
private InputStream tryClassLoader(String fullFilename, ClassLoader loader, String name) {
if (loader != null) {
Py.writeDebug("import", "trying " + fullFilename + " in " + name + " class loader");
return loader.getResourceAsStream(fullFilename);
}
return null;
}
@Override
protected String makeFilename(String fullname) {
int dot = fullname.lastIndexOf('.');
if (dot < 0) {
return fullname;
}
return fullname.substring(dot + 1);
}
@Override
protected String makeFilePath(String fullname) {
int dot = fullname.lastIndexOf('.');
if (dot < 0) {
return path;
}
return path + fullname.substring(0, dot + 1).replace('.', File.separatorChar);
}
@Override
protected String makePackagePath(String fullname) {
return path;
}
private Map<String, InputStream> entries = new HashMap<>();
private String path;
}