From cccfcf7ff1dcdd5fefdfbe3f41fe356b4ae76084 Mon Sep 17 00:00:00 2001 From: Raffi Khatchadourian Date: Sun, 24 Mar 2024 19:17:52 -0400 Subject: [PATCH] Bug fix and other updates (#173) 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."](https://github.com/wala/ML/commit/0f6872dda7bdcffdfedd884f794a366481dff0a0) 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. --- .../python/parser/PythonModuleParser.java | 32 +++++++++++++------ .../logging.properties | 1 + logging.ci.properties | 4 +-- 3 files changed, 25 insertions(+), 12 deletions(-) create mode 120000 com.ibm.wala.cast.python.test/logging.properties diff --git a/com.ibm.wala.cast.python.jython3/source/com/ibm/wala/cast/python/parser/PythonModuleParser.java b/com.ibm.wala.cast.python.jython3/source/com/ibm/wala/cast/python/parser/PythonModuleParser.java index 08d01998b..e48388e1e 100644 --- a/com.ibm.wala.cast.python.jython3/source/com/ibm/wala/cast/python/parser/PythonModuleParser.java +++ b/com.ibm.wala.cast.python.jython3/source/com/ibm/wala/cast/python/parser/PythonModuleParser.java @@ -39,7 +39,7 @@ public class PythonModuleParser extends PythonParser { - private static final Logger logger = Logger.getLogger(PythonModuleParser.class.getName()); + private static final Logger LOGGER = Logger.getLogger(PythonModuleParser.class.getName()); private final Set localModules = HashSetFactory.make(); @@ -60,9 +60,9 @@ protected PythonParser.CAstVisitor makeVisitor( return new CAstVisitor(context, parser) { @Override - public CAstNode visitImportFrom(ImportFrom arg0) throws Exception { + public CAstNode visitImportFrom(ImportFrom importFrom) throws Exception { Optional s = - arg0.getInternalModuleNames().stream() + importFrom.getInternalModuleNames().stream() .map( n -> { return n.getInternalId(); @@ -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 -> @@ -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); } }; } @@ -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)); } } @@ -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")); + } } diff --git a/com.ibm.wala.cast.python.test/logging.properties b/com.ibm.wala.cast.python.test/logging.properties new file mode 120000 index 000000000..e283a9e7e --- /dev/null +++ b/com.ibm.wala.cast.python.test/logging.properties @@ -0,0 +1 @@ +../logging.properties \ No newline at end of file diff --git a/logging.ci.properties b/logging.ci.properties index 1ff4702d0..8c6b960f9 100644 --- a/logging.ci.properties +++ b/logging.ci.properties @@ -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. @@ -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