Skip to content

Commit 99a2a11

Browse files
committed
Add conditions to allow for python executable variants (#1062)
1 parent b526232 commit 99a2a11

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

autogen/code_utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from hashlib import md5
1111
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
1212

13+
from _pytest import python
14+
1315
from autogen import oai
1416

1517
import docker
@@ -244,6 +246,8 @@ def get_powershell_command():
244246

245247

246248
def _cmd(lang: str) -> str:
249+
if lang in ["Python", "py"]:
250+
return "python"
247251
if lang.startswith("python") or lang in ["bash", "sh"]:
248252
return lang
249253
if lang in ["shell"]:

autogen/coding/local_commandline_code_executor.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CommandLineCodeRe
116116
LocalCommandLineCodeExecutor.sanitize_command(lang, code)
117117
code = silence_pip(code, lang)
118118

119+
if lang in ["Python", "py"]:
120+
lang = "python"
121+
119122
if WIN32 and lang in ["sh", "shell"]:
120123
lang = "ps1"
121124

@@ -141,7 +144,7 @@ def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CommandLineCodeRe
141144
f.write(code)
142145
file_names.append(written_file)
143146

144-
program = sys.executable if lang.startswith("python") else _cmd(lang)
147+
program = sys.executable if _cmd(lang).startswith("python") else _cmd(lang)
145148
cmd = [program, str(written_file.absolute())]
146149

147150
try:

test/coding/test_commandline_code_executor.py

+30-23
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,29 @@ def test_commandline_executor_execute_code(cls) -> None:
6464

6565

6666
def _test_execute_code(executor: CodeExecutor) -> None:
67+
68+
# Python executable variants
69+
python_variants = ["python", "Python", "py"]
70+
6771
# Test single code block.
68-
code_blocks = [CodeBlock(code="import sys; print('hello world!')", language="python")]
69-
code_result = executor.execute_code_blocks(code_blocks)
70-
assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None
72+
for py_variant in python_variants:
73+
code_blocks = [CodeBlock(code="import sys; print('hello world!')", language=py_variant)]
74+
code_result = executor.execute_code_blocks(code_blocks)
75+
assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None
7176

7277
# Test multiple code blocks.
73-
code_blocks = [
74-
CodeBlock(code="import sys; print('hello world!')", language="python"),
75-
CodeBlock(code="a = 100 + 100; print(a)", language="python"),
76-
]
77-
code_result = executor.execute_code_blocks(code_blocks)
78-
assert (
79-
code_result.exit_code == 0
80-
and "hello world!" in code_result.output
81-
and "200" in code_result.output
82-
and code_result.code_file is not None
83-
)
78+
for py_variant in python_variants:
79+
code_blocks = [
80+
CodeBlock(code="import sys; print('hello world!')", language=py_variant),
81+
CodeBlock(code="a = 100 + 100; print(a)", language=py_variant),
82+
]
83+
code_result = executor.execute_code_blocks(code_blocks)
84+
assert (
85+
code_result.exit_code == 0
86+
and "hello world!" in code_result.output
87+
and "200" in code_result.output
88+
and code_result.code_file is not None
89+
)
8490

8591
# Test bash script.
8692
if sys.platform not in ["win32"]:
@@ -89,15 +95,16 @@ def _test_execute_code(executor: CodeExecutor) -> None:
8995
assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None
9096

9197
# Test running code.
92-
file_lines = ["import sys", "print('hello world!')", "a = 100 + 100", "print(a)"]
93-
code_blocks = [CodeBlock(code="\n".join(file_lines), language="python")]
94-
code_result = executor.execute_code_blocks(code_blocks)
95-
assert (
96-
code_result.exit_code == 0
97-
and "hello world!" in code_result.output
98-
and "200" in code_result.output
99-
and code_result.code_file is not None
100-
)
98+
for py_variant in python_variants:
99+
file_lines = ["import sys", "print('hello world!')", "a = 100 + 100", "print(a)"]
100+
code_blocks = [CodeBlock(code="\n".join(file_lines), language=py_variant)]
101+
code_result = executor.execute_code_blocks(code_blocks)
102+
assert (
103+
code_result.exit_code == 0
104+
and "hello world!" in code_result.output
105+
and "200" in code_result.output
106+
and code_result.code_file is not None
107+
)
101108

102109
# Check saved code file.
103110
with open(code_result.code_file) as f:

0 commit comments

Comments
 (0)