-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathLegacyCompiler.java
More file actions
67 lines (55 loc) · 2.21 KB
/
LegacyCompiler.java
File metadata and controls
67 lines (55 loc) · 2.21 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
package org.python.compiler;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import org.python.antlr.base.mod;
import org.python.core.BytecodeLoader;
import org.python.core.CompilerFlags;
import org.python.core.Py;
import org.python.core.PyCode;
import org.python.core.PythonCodeBundle;
import org.python.core.PythonCompiler;
public class LegacyCompiler implements PythonCompiler {
public PythonCodeBundle compile(mod node, String name, String filename,
boolean linenumbers, boolean printResults, CompilerFlags cflags) {
return new LazyLegacyBundle(node, name, filename, linenumbers,
printResults, cflags);
}
private static class LazyLegacyBundle implements PythonCodeBundle {
private final mod node;
private final String name;
private final String filename;
private final boolean linenumbers;
private final boolean printResults;
private final CompilerFlags cflags;
private ByteArrayOutputStream ostream = null;
public LazyLegacyBundle(mod node, String name, String filename,
boolean linenumbers, boolean printResults, CompilerFlags cflags) {
this.node = node;
this.name = name;
this.filename = filename;
this.linenumbers = linenumbers;
this.printResults = printResults;
this.cflags = cflags;
}
public PyCode loadCode() throws Exception {
return BytecodeLoader.makeCode(name, ostream().toByteArray(),
filename);
}
public void writeTo(OutputStream stream) throws Exception {
if (this.ostream != null) {
stream.write(ostream.toByteArray());
} else {
Module.compile(node, stream, name, filename, linenumbers,
printResults, cflags);
}
}
private ByteArrayOutputStream ostream() throws Exception {
if (ostream == null) {
ostream = new ByteArrayOutputStream();
Module.compile(node, ostream, name, filename, linenumbers,
printResults, cflags);
}
return ostream;
}
}
}