vsj0702 commited on
Commit
cfca35b
·
verified ·
1 Parent(s): d48e8d9

Adding input in execute

Browse files
Files changed (1) hide show
  1. utils.py +4 -2
utils.py CHANGED
@@ -15,13 +15,15 @@ def capture_output():
15
  finally:
16
  sys.stdout, sys.stderr = old_out, old_err
17
 
18
- def execute_code(code: str) -> tuple:
19
  """
20
  Execute the provided code and return stdout, stderr and any exceptions
21
  """
22
  with capture_output() as (stdout, stderr):
23
  try:
24
- exec(code)
 
 
25
  return stdout.getvalue().strip(), stderr.getvalue().strip(), None
26
  except Exception as e:
27
  return stdout.getvalue().strip(), stderr.getvalue().strip(), str(e)
 
15
  finally:
16
  sys.stdout, sys.stderr = old_out, old_err
17
 
18
+ def execute_code(code: str, stdin: str = "") -> tuple:
19
  """
20
  Execute the provided code and return stdout, stderr and any exceptions
21
  """
22
  with capture_output() as (stdout, stderr):
23
  try:
24
+ # Simulate stdin by overriding input() calls
25
+ local_vars = {"input": lambda: stdin.splitlines().pop(0) if stdin.strip() else ""}
26
+ exec(code, {}, local_vars)
27
  return stdout.getvalue().strip(), stderr.getvalue().strip(), None
28
  except Exception as e:
29
  return stdout.getvalue().strip(), stderr.getvalue().strip(), str(e)