##// END OF EJS Templates
Prevent infinite loops in input transformation
Thomas Kluyver -
Show More
@@ -405,6 +405,9 b' def show_linewise_tokens(s: str):'
405 for tokinfo in line:
405 for tokinfo in line:
406 print(" ", tokinfo)
406 print(" ", tokinfo)
407
407
408 # Arbitrary limit to prevent getting stuck in infinite loops
409 TRANSFORM_LOOP_LIMIT = 500
410
408 class TransformerManager:
411 class TransformerManager:
409 def __init__(self):
412 def __init__(self):
410 self.cleanup_transforms = [
413 self.cleanup_transforms = [
@@ -451,11 +454,14 b' class TransformerManager:'
451 return True, transformer.transform(lines)
454 return True, transformer.transform(lines)
452
455
453 def do_token_transforms(self, lines):
456 def do_token_transforms(self, lines):
454 while True:
457 for _ in range(TRANSFORM_LOOP_LIMIT):
455 changed, lines = self.do_one_token_transform(lines)
458 changed, lines = self.do_one_token_transform(lines)
456 if not changed:
459 if not changed:
457 return lines
460 return lines
458
461
462 raise RuntimeError("Input transformation still changing after "
463 "%d iterations. Aborting." % TRANSFORM_LOOP_LIMIT)
464
459 def transform_cell(self, cell: str):
465 def transform_cell(self, cell: str):
460 if not cell.endswith('\n'):
466 if not cell.endswith('\n'):
461 cell += '\n' # Ensure the cell has a trailing newline
467 cell += '\n' # Ensure the cell has a trailing newline
General Comments 0
You need to be logged in to leave comments. Login now