From 224acb44617a2636d5a58b067cb776ee40e9d070 Mon Sep 17 00:00:00 2001 From: d_m Date: Tue, 23 Jan 2024 10:51:53 -0500 Subject: [PATCH] timeouts, etc. --- uxnbot.py | 2 +- uxnrepl.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/uxnbot.py b/uxnbot.py index 109fbe4..c47d761 100644 --- a/uxnbot.py +++ b/uxnbot.py @@ -38,7 +38,7 @@ priv_msg_re = re.compile(br':([^!]+)![^ ]+ PRIVMSG ' + nick + br' :(.*)$') ignored = {b'rst', b'wst', b''} def evaluate(msg): - output = uxnrepl.execute(msg, sandbox=sandbox) + output = uxnrepl.execute(msg.decode('utf-8'), sandbox=sandbox) lines = [s.strip() for s in output.split(b'\n')] interesting = [s for s in lines if s not in ignored] result = b' | '.join(interesting) diff --git a/uxnrepl.py b/uxnrepl.py index b8733b3..019c574 100644 --- a/uxnrepl.py +++ b/uxnrepl.py @@ -44,20 +44,24 @@ template = ''' def write_rom(path, s): f = open(path, 'w') - prog = template % s.decode('utf-8') + prog = template % s f.write(prog) f.close() -def execute(s, sandbox=None): +def execute(s, sandbox=None, timeout=2.0): _, tmp_tal = mkstemp(suffix='.tal', prefix='uxnrepl') _, tmp_rom = mkstemp(suffix='.rom', prefix='uxnrepl') write_rom(tmp_tal, s) - res = run(['uxnasm', tmp_tal, tmp_rom], cwd=sandbox, capture_output=True) - ##print('*** uxnasm.res is %r' % res) + try: + res = run(['uxnasm', tmp_tal, tmp_rom], cwd=sandbox, capture_output=True, timeout=timeout) + except TimeoutExpired: + return 'uxnasm: timed out' if res.returncode != 0: return res.stderr - res = run(['uxncli', tmp_rom], cwd=sandbox, capture_output=True) - ##print('*** uxncli.res is %r' % res) + try: + res = run(['uxncli', tmp_rom], cwd=sandbox, capture_output=True, timeout=timeout) + except TimeoutExpired: + return 'uxncli: timed out' return res.stdout def main():