15
15
# if purchased from OpenC3, Inc.
16
16
17
17
import ast
18
+ import sys
18
19
19
20
20
21
# For details on the AST, see https://docs.python.org/3/library/ast.html
@@ -47,6 +48,9 @@ class ScriptInstrumentor(ast.NodeTransformer):
47
48
def __init__ (self , filename ):
48
49
self .filename = filename
49
50
self .in_try = False
51
+ self .try_nodes = [ast .Try ]
52
+ if sys .version_info >= (3 , 11 ):
53
+ self .try_nodes .append (ast .TryStar )
50
54
51
55
# What we're trying to do is wrap executable statements in a while True try/except block
52
56
# For example if the input code is "print('HI')", we want to transform it to:
@@ -67,11 +71,11 @@ def __init__(self, filename):
67
71
def track_enter_leave (self , node ):
68
72
# Determine if we're in a try block
69
73
in_try = self .in_try
70
- if not in_try and type (node ) in ( ast . Try , ast . TryStar ) :
74
+ if not in_try and type (node ) in self . try_nodes :
71
75
self .in_try = True
72
76
# Visit the children of the node
73
77
node = self .generic_visit (node )
74
- if not in_try and type (node ) in ( ast . Try , ast . TryStar ) :
78
+ if not in_try and type (node ) in self . try_nodes :
75
79
self .in_try = False
76
80
# ast.parse returns a module, so we need to extract
77
81
# the first element of the body which is the node
@@ -129,11 +133,11 @@ def track_enter_leave(self, node):
129
133
ast .copy_location (try_node , node )
130
134
return try_node
131
135
132
- # Call the pre_line_instrumentation ONLY and then exceute the node
136
+ # Call the pre_line_instrumentation ONLY and then execute the node
133
137
def track_reached (self , node ):
134
138
# Determine if we're in a try block, this is used by track_enter_leave
135
139
in_try = self .in_try
136
- if not in_try and type (node ) in ( ast . Try , ast . TryStar ) :
140
+ if not in_try and type (node ) in self . try_nodes :
137
141
self .in_try = True
138
142
139
143
# Visit the children of the node
@@ -178,7 +182,8 @@ def track_reached(self, node):
178
182
179
183
visit_Raise = track_enter_leave
180
184
visit_Try = track_reached
181
- visit_TryStar = track_reached
185
+ if sys .version_info >= (3 , 11 ):
186
+ visit_TryStar = track_reached
182
187
visit_Assert = track_enter_leave
183
188
184
189
visit_Import = track_enter_leave
0 commit comments