Skip to content

Commit e71e445

Browse files
committedJan 21, 2025··
Fix script_isntrumentor to work with python 3.10
1 parent 56fdc7b commit e71e445

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed
 

‎openc3-cosmos-script-runner-api/scripts/script_instrumentor.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# if purchased from OpenC3, Inc.
1616

1717
import ast
18+
import sys
1819

1920

2021
# For details on the AST, see https://docs.python.org/3/library/ast.html
@@ -47,6 +48,9 @@ class ScriptInstrumentor(ast.NodeTransformer):
4748
def __init__(self, filename):
4849
self.filename = filename
4950
self.in_try = False
51+
self.try_nodes = [ast.Try]
52+
if sys.version_info >= (3, 11):
53+
self.try_nodes.append(ast.TryStar)
5054

5155
# What we're trying to do is wrap executable statements in a while True try/except block
5256
# For example if the input code is "print('HI')", we want to transform it to:
@@ -67,11 +71,11 @@ def __init__(self, filename):
6771
def track_enter_leave(self, node):
6872
# Determine if we're in a try block
6973
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:
7175
self.in_try = True
7276
# Visit the children of the node
7377
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:
7579
self.in_try = False
7680
# ast.parse returns a module, so we need to extract
7781
# the first element of the body which is the node
@@ -129,11 +133,11 @@ def track_enter_leave(self, node):
129133
ast.copy_location(try_node, node)
130134
return try_node
131135

132-
# Call the pre_line_instrumentation ONLY and then exceute the node
136+
# Call the pre_line_instrumentation ONLY and then execute the node
133137
def track_reached(self, node):
134138
# Determine if we're in a try block, this is used by track_enter_leave
135139
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:
137141
self.in_try = True
138142

139143
# Visit the children of the node
@@ -178,7 +182,8 @@ def track_reached(self, node):
178182

179183
visit_Raise = track_enter_leave
180184
visit_Try = track_reached
181-
visit_TryStar = track_reached
185+
if sys.version_info >= (3, 11):
186+
visit_TryStar = track_reached
182187
visit_Assert = track_enter_leave
183188

184189
visit_Import = track_enter_leave

0 commit comments

Comments
 (0)
Please sign in to comment.