@@ -109,14 +109,25 @@ def ast(self):
109
109
def old_py_ast (self ):
110
110
# The py_ast is the raw ast from the Python parser.
111
111
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 )
113
118
return self ._py_ast
114
119
115
120
@property
116
121
def py_ast (self ):
117
122
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
120
131
except Exception as ex :
121
132
# If that fails, try to parse the source with the new Python parser (unless it has been
122
133
# explicitly disabled).
@@ -131,7 +142,13 @@ def py_ast(self):
131
142
raise SyntaxError ("Exception %s while parsing %s" % (ex , self .path ))
132
143
else :
133
144
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 )
135
152
return self ._py_ast
136
153
except SyntaxError as ex :
137
154
raise ex
0 commit comments