Skip to content

Fix tensor dimensions for Jython 3 #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*****************************************************************************/
package com.ibm.wala.cast.python.loader;

import static java.util.logging.Level.WARNING;

import com.ibm.wala.cast.ir.translator.ConstantFoldingRewriter;
import com.ibm.wala.cast.ir.translator.RewritingTranslatorToCAst;
import com.ibm.wala.cast.ir.translator.TranslatorToCAst;
Expand All @@ -32,10 +34,15 @@
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
import org.python.core.PyObject;
import org.python.core.PySyntaxError;
import org.python.core.PyUnicode;

public class Python3Loader extends PythonLoader {

private static final Logger logger = Logger.getLogger(Python3Loader.class.getName());

public Python3Loader(IClassHierarchy cha, IClassLoader parent, List<File> pythonPath) {
super(cha, parent, pythonPath);
}
Expand Down Expand Up @@ -94,15 +101,25 @@ public ConstantFoldingRewriter createCAstRewriter(CAst ast) {
return new ConstantFoldingRewriter(ast) {
@Override
protected Object eval(CAstOperator op, Object lhs, Object rhs) {
String s = lhs + " " + op.getValue() + " " + rhs;
logger.info(() -> "Evaluating: " + s);

// Use the Python interpreter to evaluate the expression.
PyUnicode unicode = new PyUnicode(s);
PyObject x;

try {
PyObject x =
Python3Interpreter.getInterp().eval(lhs + " " + op.getValue() + " " + rhs);
if (x.isNumberType()) {
System.err.println(lhs + " " + op.getValue() + " " + rhs + " -> " + x.asInt());
return x.asInt();
}
} catch (Exception e) {
// interpreter died for some reason, so no information.
x = Python3Interpreter.getInterp().eval(unicode);
} catch (PySyntaxError e) {
// Handle syntax errors gracefully.
logger.log(WARNING, e, () -> "Syntax error in expression: " + unicode);
return null;
}

if (x.isNumberType()) {
// If the result is a number, return its integer value.
logger.info(() -> s + " -> " + x.asInt());
return x.asInt();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ public class Python3Interpreter extends com.ibm.wala.cast.python.util.PythonInte
private static PythonInterpreter interp;

public static PythonInterpreter getInterp() {
try {
if (interp == null) {
// PySystemState.initialize( );
interp = new PythonInterpreter();
}
} catch (Throwable e) {

if (interp == null) {
// PySystemState.initialize( );
interp = new PythonInterpreter();
}
return interp;
}
Expand Down
2 changes: 1 addition & 1 deletion com.ibm.wala.cast.python.ml.test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>com.ibm.wala.cast.python.jython.test</artifactId>
<artifactId>com.ibm.wala.cast.python.jython3.test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
Expand Down
Loading