Skip to content

Commit

Permalink
Bug fix and other updates (#173)
Browse files Browse the repository at this point in the history
These are some smaller updates; a larger one is coming soon but wanted to keep up-to-date with the smaller ones.

Most significant change is: [Fix bug with determining whether a module is "local."](0f6872d)

The old code didn't consider the full path of the module, which is what is contained in `localModules`. But, it still doesn't do what I think it
should do, which makes me believe that the generated CAst is incorrect. A fix is forthcoming.
  • Loading branch information
khatchad authored Mar 24, 2024
1 parent d9a3d6b commit cccfcf7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

public class PythonModuleParser extends PythonParser<ModuleEntry> {

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

private final Set<String> localModules = HashSetFactory.make();

Expand All @@ -60,9 +60,9 @@ protected PythonParser<ModuleEntry>.CAstVisitor makeVisitor(
return new CAstVisitor(context, parser) {

@Override
public CAstNode visitImportFrom(ImportFrom arg0) throws Exception {
public CAstNode visitImportFrom(ImportFrom importFrom) throws Exception {
Optional<String> s =
arg0.getInternalModuleNames().stream()
importFrom.getInternalModuleNames().stream()
.map(
n -> {
return n.getInternalId();
Expand All @@ -73,14 +73,22 @@ public CAstNode visitImportFrom(ImportFrom arg0) throws Exception {
});
if (s.isPresent()) {
String moduleName = s.get();
if (!localModules.contains(moduleName + ".py")) {
LOGGER.finer("Module name from " + importFrom + " is: " + moduleName + ".");

if (!isLocalModule(moduleName)) {
LOGGER.finer("Module: " + moduleName + ".py" + " isn't local.");
moduleName = s.get() + "/__init__";
}
if (localModules.contains(moduleName + ".py")) {
} else LOGGER.finer("Module: " + moduleName + ".py" + " is local.");

LOGGER.finer("Module name from " + importFrom + " is: " + moduleName + ".");

if (isLocalModule(moduleName)) {
LOGGER.finer("Module: " + moduleName + ".py" + " is local.");

String yuck = moduleName;
return Ast.makeNode(
CAstNode.BLOCK_STMT,
arg0.getInternalNames().stream()
importFrom.getInternalNames().stream()
.map(a -> a.getInternalName())
.map(
n ->
Expand All @@ -94,10 +102,10 @@ public CAstNode visitImportFrom(ImportFrom arg0) throws Exception {
Ast.makeConstant(yuck),
Ast.makeConstant(n))))
.collect(Collectors.toList()));
}
} else LOGGER.finer("Module: " + moduleName + ".py" + " isn't local.");
}

return super.visitImportFrom(arg0);
return super.visitImportFrom(importFrom);
}
};
}
Expand All @@ -121,7 +129,7 @@ public void accept(ModuleEntry f) {
accept(sm);
});
} else {
logger.fine(() -> "**CLS: " + scriptName((SourceModule) f));
LOGGER.fine(() -> "**CLS: " + scriptName((SourceModule) f));
localModules.add(scriptName((SourceModule) f));
}
}
Expand Down Expand Up @@ -158,4 +166,8 @@ public static void main(String[] args) throws Exception {
protected Reader getReader() throws IOException {
return new InputStreamReader(fileName.getInputStream());
}

private boolean isLocalModule(String moduleName) {
return localModules.stream().anyMatch(lm -> lm.endsWith(moduleName + ".py"));
}
}
1 change: 1 addition & 0 deletions com.ibm.wala.cast.python.test/logging.properties
4 changes: 2 additions & 2 deletions logging.ci.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ handlers= java.util.logging.ConsoleHandler
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level= INFO
.level= WARNING

############################################################
# Handler specific properties.
Expand All @@ -40,7 +40,7 @@ handlers= java.util.logging.ConsoleHandler
#java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.level = WARNING
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

# Example to customize the SimpleFormatter output format
Expand Down

0 comments on commit cccfcf7

Please sign in to comment.