commit 3b50decbe62ba58814de463f64b500a632075a51
parent e60a9ad5ad276683d10a01cfff3b1b76ca063d5c
Author: Léo Villeveygoux <l@vgx.fr>
Date: Tue, 22 Dec 2020 19:39:52 +0100
Limite du nombre de lignes exécutées
Comme un moyen d'éviter les boucles infinies.
La limite est fixée à 10000 lignes exécutées, ça pourrait s'augmenter.
Diffstat:
1 file changed, 25 insertions(+), 2 deletions(-)
diff --git a/lib/breditor.py b/lib/breditor.py
@@ -1,13 +1,36 @@
# Requires Brython and Ace editor
from browser import document, window, html
-from sys import stderr
+from sys import stderr, settrace
+
+def make_max_line_tracer(maxlines):
+ lines = 0
+ def tracer(frame, event, arg):
+ nonlocal lines
+ if event == 'line':
+ lines += 1
+ print(lines, file=stderr)
+ if lines >= maxlines:
+ raise TimeoutError
+ return tracer
+ return tracer
def exec_code(editor, id):
stderr_frame = document[id + "_stderr"]
stderr_frame.clear()
stderr.write = lambda data : stderr_target(data, stderr_frame)
- exec(editor.getValue())
+
+ code = editor.getValue()
+ compiled = compile(code, "<" + id + ">", "exec")
+
+ settrace(make_max_line_tracer(10000)) # increase to allow longer execution
+ try:
+ exec(compiled)
+ except TimeoutError:
+ settrace(None)
+ print("L'exécution prend trop de temps, abandon.", file=stderr)
+ finally:
+ settrace(None)
def stderr_target(data, elt):
elt <= data