Skip to content

Commit 9b678bc

Browse files
authored
Merge pull request #150 from ponder-lab/42-supporting-python-3-causes-tensor-dimensions-not-to-be-calculated
Progress on tensor shape resolution for Python 3, wala#42.
2 parents ae33039 + c9a3d9f commit 9b678bc

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

com.ibm.wala.cast.python.jython3/source/com/ibm/wala/cast/python/loader/Python3Loader.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*****************************************************************************/
1111
package com.ibm.wala.cast.python.loader;
1212

13+
import static java.util.logging.Level.WARNING;
14+
1315
import com.ibm.wala.cast.ir.translator.ConstantFoldingRewriter;
1416
import com.ibm.wala.cast.ir.translator.RewritingTranslatorToCAst;
1517
import com.ibm.wala.cast.ir.translator.TranslatorToCAst;
@@ -32,10 +34,15 @@
3234
import java.io.File;
3335
import java.io.IOException;
3436
import java.util.List;
37+
import java.util.logging.Logger;
3538
import org.python.core.PyObject;
39+
import org.python.core.PySyntaxError;
40+
import org.python.core.PyUnicode;
3641

3742
public class Python3Loader extends PythonLoader {
3843

44+
private static final Logger logger = Logger.getLogger(Python3Loader.class.getName());
45+
3946
public Python3Loader(IClassHierarchy cha, IClassLoader parent, List<File> pythonPath) {
4047
super(cha, parent, pythonPath);
4148
}
@@ -94,15 +101,25 @@ public ConstantFoldingRewriter createCAstRewriter(CAst ast) {
94101
return new ConstantFoldingRewriter(ast) {
95102
@Override
96103
protected Object eval(CAstOperator op, Object lhs, Object rhs) {
104+
String s = lhs + " " + op.getValue() + " " + rhs;
105+
logger.info(() -> "Evaluating: " + s);
106+
107+
// Use the Python interpreter to evaluate the expression.
108+
PyUnicode unicode = new PyUnicode(s);
109+
PyObject x;
110+
97111
try {
98-
PyObject x =
99-
Python3Interpreter.getInterp().eval(lhs + " " + op.getValue() + " " + rhs);
100-
if (x.isNumberType()) {
101-
System.err.println(lhs + " " + op.getValue() + " " + rhs + " -> " + x.asInt());
102-
return x.asInt();
103-
}
104-
} catch (Exception e) {
105-
// interpreter died for some reason, so no information.
112+
x = Python3Interpreter.getInterp().eval(unicode);
113+
} catch (PySyntaxError e) {
114+
// Handle syntax errors gracefully.
115+
logger.log(WARNING, e, () -> "Syntax error in expression: " + unicode);
116+
return null;
117+
}
118+
119+
if (x.isNumberType()) {
120+
// If the result is a number, return its integer value.
121+
logger.info(() -> s + " -> " + x.asInt());
122+
return x.asInt();
106123
}
107124
return null;
108125
}

com.ibm.wala.cast.python.jython3/source/com/ibm/wala/cast/python/util/Python3Interpreter.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@ public class Python3Interpreter extends com.ibm.wala.cast.python.util.PythonInte
77
private static PythonInterpreter interp;
88

99
public static PythonInterpreter getInterp() {
10-
try {
11-
if (interp == null) {
12-
// PySystemState.initialize( );
13-
interp = new PythonInterpreter();
14-
}
15-
} catch (Throwable e) {
16-
10+
if (interp == null) {
11+
// PySystemState.initialize( );
12+
interp = new PythonInterpreter();
1713
}
1814
return interp;
1915
}

com.ibm.wala.cast.python.ml.test/source/com/ibm/wala/cast/python/ml/test/TestNeuroImageExamples.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ public void testEx1CG()
2121
Ex1URL,
2222
(PropagationCallGraphBuilder cgBuilder, CallGraph CG, TensorTypeAnalysis result) -> {
2323
String in = "[{[D:Constant,64000] of pixel}]";
24-
String out = "[{[D:Constant,40, D:Constant,40, D:Constant,40, D:Constant,1] of pixel}]";
25-
// NOTE: Change last two arguments to `in`, `out` once
26-
// https://github.com/wala/ML/issues/42 is fixed.
27-
checkTensorOp(cgBuilder, CG, result, "reshape", null, null);
24+
// NOTE: Change to "[{[D:Constant,40, D:Constant,40, D:Constant,40, D:Constant,1] of
25+
// pixel}]" once https://github.com/wala/ML/issues/195 is fixed.
26+
String out = "[{[D:Symbolic,n, D:Compound,[D:Constant,28, D:Constant,28]] of pixel}]";
27+
checkTensorOp(cgBuilder, CG, result, "reshape", in, out);
2828

2929
in = "[{[D:Constant,40, D:Constant,40, D:Constant,40, D:Constant,1] of pixel}]";
30-
// NOTE: Change next to last argument to `in` once https://github.com/wala/ML/issues/42 is
31-
// fixed.
30+
// NOTE: Change next to last argument to `in` once https://github.com/wala/ML/issues/195
31+
// is fixed.
3232
checkTensorOp(cgBuilder, CG, result, "conv3d", null, null);
3333
});
3434
}

com.ibm.wala.cast.python.test/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151
<groupId>junit</groupId>
5252
<artifactId>junit</artifactId>
5353
</dependency>
54+
<dependency>
55+
<groupId>org.ow2.asm</groupId>
56+
<artifactId>asm-all</artifactId>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.github.jnr</groupId>
60+
<artifactId>jnr-constants</artifactId>
61+
</dependency>
5462
</dependencies>
5563
<build>
5664
<resources>

0 commit comments

Comments
 (0)