valiant21 commited on
Commit
986c159
·
verified ·
1 Parent(s): 0410277

input methods and def update

Browse files
Files changed (1) hide show
  1. interpreter.py +18 -8
interpreter.py CHANGED
@@ -6,13 +6,13 @@ from logger import logger
6
  def run_code(code):
7
  """
8
  Executes user-provided Python code and captures its output.
9
- Detects function definitions and provides feedback if functions are not invoked.
10
 
11
  Parameters:
12
  code (str): Python code entered by the user.
13
 
14
  Returns:
15
- str: Captured output or error messages.
16
  """
17
  # Redirect stdout to capture code output
18
  old_stdout = sys.stdout
@@ -21,6 +21,18 @@ def run_code(code):
21
  # Create a dedicated execution namespace
22
  exec_globals = {}
23
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  try:
25
  # Parse the code to detect function definitions
26
  tree = ast.parse(code)
@@ -32,18 +44,16 @@ def run_code(code):
32
  exec(code, exec_globals)
33
 
34
  # Check if functions are defined but not called
 
35
  if function_names:
36
- captured_output = redirected_output.getvalue()
37
  captured_output += f"\n\nDefined functions: {', '.join(function_names)}\n"
38
  captured_output += "Note: Functions need to be explicitly called to see their output."
39
- return captured_output
 
40
 
41
  except Exception as e:
42
  logger.error(f"Execution error: {e}")
43
- return f"Error: {e}"
44
  finally:
45
  # Reset stdout
46
  sys.stdout = old_stdout
47
-
48
- # Return the captured output
49
- return redirected_output.getvalue()
 
6
  def run_code(code):
7
  """
8
  Executes user-provided Python code and captures its output.
9
+ Handles user input dynamically and detects defined functions.
10
 
11
  Parameters:
12
  code (str): Python code entered by the user.
13
 
14
  Returns:
15
+ dict: Captured output and logged inputs.
16
  """
17
  # Redirect stdout to capture code output
18
  old_stdout = sys.stdout
 
21
  # Create a dedicated execution namespace
22
  exec_globals = {}
23
 
24
+ # Store user inputs
25
+ input_log = []
26
+
27
+ # Custom input function to simulate input() behavior
28
+ def custom_input(prompt=""):
29
+ user_input = input(prompt) # Prompt the user dynamically
30
+ input_log.append(user_input)
31
+ return user_input
32
+
33
+ # Add the custom input function to the execution namespace
34
+ exec_globals["input"] = custom_input
35
+
36
  try:
37
  # Parse the code to detect function definitions
38
  tree = ast.parse(code)
 
44
  exec(code, exec_globals)
45
 
46
  # Check if functions are defined but not called
47
+ captured_output = redirected_output.getvalue()
48
  if function_names:
 
49
  captured_output += f"\n\nDefined functions: {', '.join(function_names)}\n"
50
  captured_output += "Note: Functions need to be explicitly called to see their output."
51
+
52
+ return {"output": captured_output, "inputs": input_log}
53
 
54
  except Exception as e:
55
  logger.error(f"Execution error: {e}")
56
+ return {"output": f"Error: {e}", "inputs": input_log}
57
  finally:
58
  # Reset stdout
59
  sys.stdout = old_stdout