Skip to content

Commit 0bb5b4b

Browse files
authored
Merge pull request github#17875 from github/tausbn/python-improve-parser-logging-and-timing
Python: Improve parser logging/timing/customisability
2 parents 2892f0f + 2ef3ae9 commit 0bb5b4b

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

python/extractor/semmle/python/imports.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def get_imports(self, module, loaded_module):
9898
def get_import_nodes(self, loaded_module):
9999
'Return list of AST nodes representing imports'
100100
try:
101+
self.logger.debug("Looking for imports in %s", loaded_module.path)
101102
return imports_from_ast(loaded_module.py_ast)
102103
except Exception as ex:
103104
if isinstance(ex, SyntaxError):

python/extractor/semmle/python/modules.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,25 @@ def ast(self):
109109
def old_py_ast(self):
110110
# The py_ast is the raw ast from the Python parser.
111111
if self._py_ast is None:
112-
self._py_ast = semmle.python.parser.parse(self.tokens, self.logger)
112+
with timers["old_py_ast"]:
113+
self.logger.debug("Trying old parser on %s", self.path)
114+
self._py_ast = semmle.python.parser.parse(self.tokens, self.logger)
115+
self.logger.debug("Old parser successful on %s", self.path)
116+
else:
117+
self.logger.debug("Found (during old_py_ast) parse tree for %s in cache", self.path)
113118
return self._py_ast
114119

115120
@property
116121
def py_ast(self):
117122
try:
118-
# First, try to parse the source with the old Python parser.
119-
return self.old_py_ast
123+
# If the `CODEQL_PYTHON_DISABLE_OLD_PARSER` flag is present, we do not try to use the
124+
# old parser, and instead jump straight to the exception handler.
125+
if os.environ.get("CODEQL_PYTHON_DISABLE_OLD_PARSER"):
126+
self.logger.debug("Old parser disabled, skipping old parse attempt for %s", self.path)
127+
raise Exception("Skipping old parser")
128+
# Otherwise, we first try to parse the source with the old Python parser.
129+
self._py_ast = self.old_py_ast
130+
return self._py_ast
120131
except Exception as ex:
121132
# If that fails, try to parse the source with the new Python parser (unless it has been
122133
# explicitly disabled).
@@ -131,7 +142,13 @@ def py_ast(self):
131142
raise SyntaxError("Exception %s while parsing %s" % (ex, self.path))
132143
else:
133144
try:
134-
self._py_ast = semmle.python.parser.tsg_parser.parse(self.path, self.logger)
145+
with timers["tsg_py_ast"]:
146+
if self._py_ast is None:
147+
self.logger.debug("Trying tsg-python on %s", self.path)
148+
self._py_ast = semmle.python.parser.tsg_parser.parse(self.path, self.logger)
149+
self.logger.debug("tsg-python successful on %s", self.path)
150+
else:
151+
self.logger.debug("Found (during py_ast) parse tree for %s in cache", self.path)
135152
return self._py_ast
136153
except SyntaxError as ex:
137154
raise ex

python/extractor/semmle/python/parser/tsg_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def read_tsg_python_output(path, logger):
168168
p.stdout.close()
169169
p.terminate()
170170
p.wait()
171-
logger.info("Read {} nodes and {} edges from TSG output".format(len(node_attr), len(edge_attr)))
171+
logger.debug("Read {} nodes and {} edges from TSG output".format(len(node_attr), len(edge_attr)))
172172
return node_attr, edge_attr
173173

174174
def evaluate_string(s):

python/extractor/semmle/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#Semantic version of extractor.
1212
#Update this if any changes are made
13-
VERSION = "7.1.0"
13+
VERSION = "7.1.1"
1414

1515
PY_EXTENSIONS = ".py", ".pyw"
1616

0 commit comments

Comments
 (0)