Skip to content

Commit

Permalink
Fixed python interpreter edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
KillianLucas committed Aug 13, 2023
1 parent 5fb4049 commit 3c2bd5d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 34 deletions.
42 changes: 10 additions & 32 deletions interpreter/code_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,31 +154,29 @@ def run(self):

# Add print commands that tell us what the active line is
if self.print_cmd:
code = self.add_active_line_prints(code)

# If it's Python, we also need to prepare it for `python -i`
if self.language == "python":

# Normalize code by parsing then unparsing it
try:
code = prepare_for_python_interactive(code)
code = self.add_active_line_prints(code)
except:
# If this failed, it means the code didn't compile
# This traceback will be our output.

traceback_string = traceback.format_exc()
self.output = traceback_string
self.update_active_block()

# Before you return, wait for the display to catch up?
# (I'm not sure why this works)
time.sleep(0.1)

return self.output

code = fix_code_indentation(code)

if self.language == "python":
# This lets us stop execution when error happens (which is not default -i behavior)
# And solves a bunch of indentation problems-- if everything's indented, -i treats it as one block
code = wrap_in_try_except(code)

# Remove any whitespace lines, as this will break indented blocks
# (are we sure about this? test this)
code_lines = code.split("\n")
code_lines = [c for c in code_lines if c.strip() != ""]
code = "\n".join(code_lines)
Expand All @@ -202,7 +200,7 @@ def run(self):
print(code)
print("---")

# HTML-specific processing
# HTML-specific processing (and running)
if self.language == "html":
output = language_map["html"]["run_function"](code)
return output
Expand Down Expand Up @@ -330,20 +328,6 @@ def save_and_display_stream(self, stream):

self.update_active_block()

def fix_code_indentation(code):
lines = code.split("\n")
fixed_lines = []
was_indented = False
for line in lines:
current_indent = len(line) - len(line.lstrip())
if current_indent == 0 and was_indented:
fixed_lines.append('') # Add an empty line after an indented block
fixed_lines.append(line)
was_indented = current_indent > 0

return "\n".join(fixed_lines)


def truncate_output(data):

# In the future, this will come from a config file
Expand Down Expand Up @@ -425,12 +409,6 @@ def add_active_line_prints_to_python(code):
new_tree = transformer.visit(tree)
return ast.unparse(new_tree)

def prepare_for_python_interactive(code):
"""
Adjusts code formatting for the python -i flag.
"""
return wrap_in_try_except(code)

def wrap_in_try_except(code):
# Add import traceback
code = "import traceback\n" + code
Expand Down
3 changes: 1 addition & 2 deletions interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ def get_info_for_system_message(self):
query = query[-2000:]

# Use them to query Open Procedures
# url = f"https://open-procedures.replit.app/search/?query={query}" # <-- Prod URL
url = f"https://open-procedures.killianlucas1.repl.co/search/?query={query}" # <-- Test URL
url = f"https://open-procedures.replit.app/search/?query={query}"
relevant_procedures = requests.get(url).json()["procedures"]
info += "\n\n# Potentially Helpful Procedures (may or may not be related)\n" + "\n---\n".join(relevant_procedures)

Expand Down

0 comments on commit 3c2bd5d

Please sign in to comment.