Skip to content

Commit

Permalink
Fix script_isntrumentor to work with python 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthomas committed Jan 21, 2025
1 parent 56fdc7b commit e71e445
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions openc3-cosmos-script-runner-api/scripts/script_instrumentor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# if purchased from OpenC3, Inc.

import ast
import sys


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

# What we're trying to do is wrap executable statements in a while True try/except block
# For example if the input code is "print('HI')", we want to transform it to:
Expand All @@ -67,11 +71,11 @@ def __init__(self, filename):
def track_enter_leave(self, node):
# Determine if we're in a try block
in_try = self.in_try
if not in_try and type(node) in (ast.Try, ast.TryStar):
if not in_try and type(node) in self.try_nodes:
self.in_try = True
# Visit the children of the node
node = self.generic_visit(node)
if not in_try and type(node) in (ast.Try, ast.TryStar):
if not in_try and type(node) in self.try_nodes:
self.in_try = False
# ast.parse returns a module, so we need to extract
# the first element of the body which is the node
Expand Down Expand Up @@ -129,11 +133,11 @@ def track_enter_leave(self, node):
ast.copy_location(try_node, node)
return try_node

# Call the pre_line_instrumentation ONLY and then exceute the node
# Call the pre_line_instrumentation ONLY and then execute the node
def track_reached(self, node):
# Determine if we're in a try block, this is used by track_enter_leave
in_try = self.in_try
if not in_try and type(node) in (ast.Try, ast.TryStar):
if not in_try and type(node) in self.try_nodes:
self.in_try = True

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

visit_Raise = track_enter_leave
visit_Try = track_reached
visit_TryStar = track_reached
if sys.version_info >= (3, 11):
visit_TryStar = track_reached
visit_Assert = track_enter_leave

visit_Import = track_enter_leave
Expand Down

0 comments on commit e71e445

Please sign in to comment.