encode timeout msg in bytes (#1078)

* encode timeout msg in bytes

* fix msg and test
This commit is contained in:
Chi Wang 2023-06-12 11:07:14 -07:00 committed by GitHub
parent a30d198530
commit c5dfb03f0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 6 deletions

View File

@ -13,6 +13,7 @@ from flaml.autogen import oai, DEFAULT_MODEL, FAST_MODEL
CODE_BLOCK_PATTERN = r"```(\w*)\n(.*?)\n```"
WORKING_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "extensions")
UNKNOWN = "unknown"
TIMEOUT_MSG = bytes("Timeout", "utf-8")
def infer_lang(code):
@ -180,7 +181,6 @@ def execute_code(
filepath = os.path.join(work_dir, filename)
file_dir = os.path.dirname(filepath)
os.makedirs(file_dir, exist_ok=True)
if code is not None:
with open(filepath, "w") as fout:
fout.write(code)
@ -202,7 +202,7 @@ def execute_code(
except TimeoutError:
if original_filename is None:
os.remove(filepath)
return 1, "Timeout", None
return 1, TIMEOUT_MSG, None
if original_filename is None:
os.remove(filepath)
return result.returncode, result.stderr if result.returncode else result.stdout, None
@ -260,7 +260,7 @@ def execute_code(
container.remove()
if original_filename is None:
os.remove(filepath)
return 1, "Timeout", image
return 1, TIMEOUT_MSG, image
# try:
# container.wait(timeout=timeout)
# except (ReadTimeout, ConnectionError):

View File

@ -1 +1 @@
__version__ = "2.0.0rc1"
__version__ = "2.0.0rc2"

View File

@ -74,9 +74,9 @@ def test_execute_code():
assert exit_code, msg
# execute code which takes a long time
exit_code, error, image = execute_code("import time; time.sleep(2)", timeout=1)
assert exit_code and error == "Timeout"
assert exit_code and error.decode() == "Timeout"
exit_code, error, image = execute_code("import time; time.sleep(2)", timeout=1, use_docker=False)
assert exit_code and error == "Timeout" and image is None
assert exit_code and error.decode() == "Timeout" and image is None
if __name__ == "__main__":