monty 0.0.21

A sandboxed, snapshotable Python interpreter written in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# Control flow (break/continue/return) interacting with finally blocks,
# especially on the exception path — regression tests for
# https://github.com/pydantic/monty/issues/647 (finally ran twice when
# entered via an in-flight exception and exited via break/return).

# === break in finally swallows an in-flight exception, finally runs once ===
xs = []
while True:
    try:
        raise ValueError
    finally:
        xs.append(1)
        break
assert xs == [1]

# === return in finally swallows an in-flight exception, finally runs once ===
xs = []


def return_in_finally():
    try:
        raise ValueError
    finally:
        xs.append(1)
        return 2


assert return_in_finally() == 2
assert xs == [1]

# === continue in finally swallows an in-flight exception ===
xs = []
for i in range(3):
    try:
        raise ValueError
    finally:
        xs.append(i)
        continue
assert xs == [0, 1, 2]


# === after a swallow, there is no active exception to re-raise ===
def swallow_then_bare_raise():
    while True:
        try:
            raise ValueError('swallowed')
        finally:
            break
    raise


try:
    swallow_then_bare_raise()
    assert False, 'expected RuntimeError'
except RuntimeError as e:
    assert str(e) == 'No active exception to reraise'

# === finally raising on the return path runs once and propagates ===
xs = []


def raise_in_finally_during_return():
    try:
        return 1
    finally:
        xs.append(1)
        raise ValueError('boom')


try:
    raise_in_finally_during_return()
    assert False, 'expected ValueError'
except ValueError as e:
    assert str(e) == 'boom'
assert xs == [1]

# === return value is evaluated before the finally body runs ===
order = []


def note(tag, value):
    order.append(tag)
    return value


def return_evaluation_order():
    try:
        return note('value', 42)
    finally:
        order.append('finally')


assert return_evaluation_order() == 42
assert order == ['value', 'finally']


# === break in finally discards a pending return value ===
def break_discards_return():
    for i in range(3):
        try:
            return 1
        finally:
            break
    return 'after loop'


assert break_discards_return() == 'after loop'


# === continue in finally discards a pending return value ===
def continue_discards_return():
    runs = []
    for i in range(2):
        try:
            return 1
        finally:
            runs.append(i)
            continue
    return runs


assert continue_discards_return() == [0, 1]


# === return in finally overrides return in try ===
def finally_overrides_return():
    try:
        return 'try'
    finally:
        return 'finally'


assert finally_overrides_return() == 'finally'

# === nested finallys each run once, inner first, on break ===
xs = []
while True:
    try:
        try:
            raise ValueError
        finally:
            xs.append('inner')
    finally:
        xs.append('outer')
        break
assert xs == ['inner', 'outer']

# === break crossing an inner finally from inside an outer finally ===
xs = []
while True:
    try:
        raise ValueError
    finally:
        try:
            xs.append('inner-try')
        finally:
            xs.append('inner-finally')
        xs.append('outer-finally')
        break
assert xs == ['inner-try', 'inner-finally', 'outer-finally']

# === exception raised inside a finally during break is caught outside ===
xs = []
try:
    while True:
        try:
            break
        finally:
            xs.append('finally')
            raise ValueError('from finally')
except ValueError as e:
    xs.append(str(e))
assert xs == ['finally', 'from finally']

# === try/except nested inside an exception-path finally ===
# The inner handler must not disturb the in-flight exception: the outer
# except still sees the original, and a bare raise in the finally would too.
out = []
try:
    try:
        raise ValueError('original')
    finally:
        try:
            raise TypeError('inner')
        except TypeError:
            pass
        out.append('finally done')
except ValueError as e:
    out.append(str(e))
assert out == ['finally done', 'original']


# === bare raise in a finally inside an except handler sees the handler's exception ===
def bare_raise_in_inner_finally():
    seen = []
    try:
        raise ValueError('original')
    except ValueError:
        try:
            return seen
        finally:
            try:
                raise
            except ValueError as e:
                seen.append(str(e))


assert bare_raise_in_inner_finally() == ['original']

# === handler variable is unbound after break out of the handler ===
for i in range(3):
    try:
        raise ValueError('x')
    except ValueError as caught:
        break
try:
    caught
    assert False, 'expected NameError'
except NameError:
    pass


# === handler target is cleared before an exceptional exit reaches finally ===
handler_cleanup = []
try:
    try:
        try:
            raise ValueError('original')
        except ValueError as abandoned:
            raise TypeError('replacement')
    finally:
        try:
            abandoned
        except NameError:
            handler_cleanup.append('unbound')
        else:
            handler_cleanup.append('bound')
except TypeError as replacement:
    handler_cleanup.append(str(replacement))
assert handler_cleanup == ['unbound', 'replacement']


# === captured handler target is cleared on an exceptional exit ===
# The cleanup path for a replacement exception uses DeleteCell when a closure
# captures the handler target; normal fall-through and return use other paths.
def exceptional_handler_cell_cleanup():
    probe = None
    try:
        try:
            raise ValueError('original')
        except ValueError as captured:
            probe = lambda: captured
            raise TypeError('replacement')
    except TypeError:
        pass

    try:
        probe()
        return 'still bound'
    except NameError as e:
        return str(e)


assert exceptional_handler_cell_cleanup() == (
    "cannot access free variable 'captured' where it is not associated with a value in enclosing scope"
)


# === handler variable is unbound after return out of the handler ===
# the probe closure captures `exc2`, so calling it after the return observes
# the handler cleanup unbinding the cell (a module-level lookup could not —
# it would be NameError regardless)


def return_from_handler():
    try:
        raise ValueError('x')
    except ValueError as exc2:
        return 'returned', lambda: exc2


value, probe = return_from_handler()
assert value == 'returned'
try:
    probe()
    assert False, 'expected NameError'
except NameError:
    pass


# === owning frame reads the handler variable after cleanup: UnboundLocalError ===
# only the nested closure's read (above) is the free-variable NameError; the
# frame that owns the cell gets UnboundLocalError, like any unassigned local
def owner_read_after_cleanup():
    try:
        raise ValueError('x')
    except ValueError as exc3:
        hold = lambda: exc3
    return exc3


try:
    owner_read_after_cleanup()
    assert False, 'expected UnboundLocalError'
except UnboundLocalError as e:
    assert str(e) == "cannot access local variable 'exc3' where it is not associated with a value"


# === handler variable is unbound after continue out of the handler ===
for i in range(2):
    try:
        raise ValueError('x')
    except ValueError as caught2:
        continue
try:
    caught2
    assert False, 'expected NameError'
except NameError:
    pass

# === with + finally: __exit__ and finally each run once on break ===
events = []


class Tracker:
    def __enter__(self):
        events.append('enter')
        return self

    def __exit__(self, typ, val, tb):
        events.append('exit')
        return False


while True:
    try:
        with Tracker():
            raise ValueError
    finally:
        events.append('finally')
        break
assert events == ['enter', 'exit', 'finally']

# === __exit__ failure replaces return and is caught by the enclosing region ===
events = []


class ExplodingExit:
    def __init__(self, tag):
        self.tag = tag

    def __enter__(self):
        events.append(f'enter {self.tag}')
        return self

    def __exit__(self, typ, val, tb):
        events.append(f'exit {self.tag}')
        raise RuntimeError(f'exit failed {self.tag}')


def return_through_exploding_exit():
    try:
        with ExplodingExit('return'):
            return 'discarded'
    except RuntimeError as e:
        return str(e)


assert return_through_exploding_exit() == 'exit failed return'
assert events == ['enter return', 'exit return']

# === __exit__ failure replaces break and is caught outside the loop ===
events = []
try:
    while True:
        with ExplodingExit('break'):
            break
except RuntimeError as e:
    events.append(str(e))
assert events == ['enter break', 'exit break', 'exit failed break']

# === break inside with inside finally calls __exit__ once ===
events = []
while True:
    try:
        raise ValueError
    finally:
        with Tracker():
            events.append('body')
            break
assert events == ['enter', 'body', 'exit']

# === return through with + for + finally unwinds innermost first ===
events = []


def deep_unwind():
    try:
        for i in range(5):
            with Tracker():
                return 'done'
    finally:
        events.append('finally')


assert deep_unwind() == 'done'
assert events == ['enter', 'exit', 'finally']


# === exception in the exception-path finally replaces the original ===
def replace_exception():
    try:
        raise ValueError('original')
    finally:
        raise TypeError('replacement')


try:
    replace_exception()
    assert False, 'expected TypeError'
except TypeError as e:
    assert str(e) == 'replacement'

# === finally runs once per path in try/except/else/finally ===
xs = []
for mode in ['ok', 'caught', 'uncaught', 'ret']:

    def run(mode=mode):
        try:
            if mode in ('caught', 'uncaught'):
                raise KeyError(mode)
        except KeyError as e:
            xs.append(f'except {mode}')
            if mode == 'uncaught':
                raise ValueError(mode)
        else:
            xs.append(f'else {mode}')
            if mode == 'ret':
                return 'ret'
        finally:
            xs.append(f'finally {mode}')

    try:
        run()
    except ValueError:
        xs.append(f'caught {mode}')
assert xs == [
    'else ok',
    'finally ok',
    'except caught',
    'finally caught',
    'except uncaught',
    'finally uncaught',
    'caught uncaught',
    'else ret',
    'finally ret',
]

# === break through try/except/finally from the handler body ===
xs = []
while True:
    try:
        raise ValueError
    except ValueError:
        xs.append('except')
        break
    finally:
        xs.append('finally')
assert xs == ['except', 'finally']


# === continue clears a named handler before the enclosing finally runs ===
def continue_from_handler_through_finally():
    seen = []
    for i in range(2):
        try:
            try:
                raise ValueError(str(i))
            except ValueError as current:
                seen.append(f'handler {current}')
                continue
        finally:
            try:
                current
                seen.append('bound')
            except UnboundLocalError:
                seen.append('unbound')
            seen.append(f'finally {i}')
    return seen


assert continue_from_handler_through_finally() == [
    'handler 0',
    'unbound',
    'finally 0',
    'handler 1',
    'unbound',
    'finally 1',
]

# === continue in exception-path finally keeps looping, then loop ends normally ===
xs = []
for i in range(2):
    try:
        raise ValueError(str(i))
    finally:
        xs.append(i)
        continue
else:
    xs.append('else')
assert xs == [0, 1, 'else']

# === break through finally skips the loop else block ===
xs = []
for i in range(2):
    try:
        raise ValueError
    finally:
        break
else:
    xs.append('else')
assert xs == []

# === break targeting a loop *inside* the finally does not swallow ===
# The loop lives entirely within the finally body, so its break must not
# discard the in-flight exception.
xs = []
try:
    try:
        raise ValueError('kept')
    finally:
        for k in range(5):
            xs.append(k)
            break
        xs.append('after inner loop')
except ValueError as e:
    xs.append(str(e))
assert xs == [0, 'after inner loop', 'kept']


# === break targeting a loop inside an except body keeps handler state ===
def break_in_handler_loop():
    try:
        raise ValueError('original')
    except ValueError:
        while True:
            break
        try:
            raise
        except ValueError as e:
            return str(e)


assert break_in_handler_loop() == 'original'


# === for+break inside an except body, then bare raise propagates ===
def for_break_then_reraise():
    try:
        raise ValueError('kept by handler')
    except ValueError:
        for i in [1, 2]:
            break
        raise


try:
    for_break_then_reraise()
    assert False, 'expected ValueError'
except ValueError as e:
    assert str(e) == 'kept by handler'


# === return crossing two nested finallys runs both, inner first ===
order = []


def return_two_finallys():
    try:
        try:
            return 'value'
        finally:
            order.append('inner')
    finally:
        order.append('outer')


assert return_two_finallys() == 'value'
assert order == ['inner', 'outer']

# === return in inner finally swallows exception, still crosses outer finally ===
order = []


def swallow_then_outer():
    try:
        try:
            raise ValueError
        finally:
            order.append('inner')
            return 'swallowed'
    finally:
        order.append('outer')


assert swallow_then_outer() == 'swallowed'
assert order == ['inner', 'outer']

# === break from a non-first except handler runs the finally ===
xs = []
while True:
    try:
        raise KeyError('k')
    except ValueError:
        xs.append('wrong handler')
    except KeyError:
        xs.append('key')
        break
    finally:
        xs.append('finally')
assert xs == ['key', 'finally']

# === break inside with inside except handler unwinds both ===
events = []


class Recorder:
    def __enter__(self):
        events.append('enter')
        return self

    def __exit__(self, typ, val, tb):
        events.append(f'exit {typ is None}')
        return False


while True:
    try:
        raise ValueError
    except ValueError:
        with Recorder():
            break
assert events == ['enter', 'exit True']


# === __exit__ swallowing the exception, then loop control flow continues ===
class Swallow:
    def __enter__(self):
        return self

    def __exit__(self, typ, val, tb):
        return True


xs = []
for i in range(3):
    with Swallow():
        raise ValueError(str(i))
    xs.append(f'resumed {i}')
assert xs == ['resumed 0', 'resumed 1', 'resumed 2']

xs = []
for i in range(3):
    with Swallow():
        if i == 1:
            raise ValueError('swallowed')
        xs.append(i)
    xs.append(f'after {i}')
assert xs == [0, 'after 0', 'after 1', 2, 'after 2']

# === try/finally inside with body: break swallows, __exit__ sees no exception ===
events = []
while True:
    with Recorder():
        try:
            raise ValueError
        finally:
            events.append('finally')
            break
assert events == ['enter', 'finally', 'exit True']

# === exception propagating out of with body reaches __exit__ with the type ===
events = []
try:
    with Recorder():
        raise ValueError('through exit')
except ValueError as e:
    events.append(str(e))
assert events == ['enter', 'exit False', 'through exit']

# === for → with → try/finally → return unwinds innermost first ===
events = []


def deep_mixed():
    for i in range(3):
        with Recorder():
            try:
                return 'deep'
            finally:
                events.append('finally')


assert deep_mixed() == 'deep'
assert events == ['enter', 'finally', 'exit True']

# === continue crossing with + finally in one step ===
events = []
for i in range(2):
    with Recorder():
        try:
            events.append(i)
            continue
        finally:
            events.append('finally')
assert events == ['enter', 0, 'finally', 'exit True', 'enter', 1, 'finally', 'exit True']

# === exception raised by the call in `return f()` still reaches handlers ===
# Regression: the return's unwind used to interrupt the protected region at
# exactly the callee's exception-delivery offset (the instruction after the
# call), so except/finally/__exit__ were all skipped when the call raised.


def boom():
    raise ValueError('boom')


def return_call_except():
    try:
        return boom()
    except ValueError as e:
        return f'caught {e}'


assert return_call_except() == 'caught boom'


def return_call_short_circuit():
    try:
        return False or boom()
    except ValueError as e:
        return f'caught {e}'


assert return_call_short_circuit() == 'caught boom'


# Attribute and indirect calls have distinct bytecode call shapes, while `and`
# reaches its right operand through the same fall-through path as `or`.
class ReturnBoom:
    def fail(self):
        raise ValueError('attribute boom')


def return_attr_call_except():
    try:
        return ReturnBoom().fail()
    except ValueError as e:
        return f'caught {e}'


assert return_attr_call_except() == 'caught attribute boom'


def get_boom():
    return boom


def return_indirect_call_except():
    try:
        return get_boom()()
    except ValueError as e:
        return f'caught {e}'


assert return_indirect_call_except() == 'caught boom'


def return_call_and_fallthrough():
    try:
        return True and boom()
    except ValueError as e:
        return f'caught {e}'


assert return_call_and_fallthrough() == 'caught boom'


def return_call_ternary_fallthrough():
    try:
        return 0 if False else boom()
    except ValueError as e:
        return f'caught {e}'


assert return_call_ternary_fallthrough() == 'caught boom'


def return_call_ternary_jump():
    try:
        return boom() if True else 0
    except ValueError as e:
        return f'caught {e}'


assert return_call_ternary_jump() == 'caught boom'

events = []


def return_call_finally():
    try:
        return boom()
    finally:
        events.append('finally')


try:
    return_call_finally()
except ValueError as e:
    events.append(str(e))
assert events == ['finally', 'boom']

events = []


def return_call_with():
    with Recorder():
        return boom()


try:
    return_call_with()
except ValueError:
    events.append('caught')
assert events == ['enter', 'exit False', 'caught']

events = []


def return_call_nested():
    try:
        try:
            return boom()
        except ValueError:
            events.append('inner')
            raise
    finally:
        events.append('finally')


try:
    return_call_nested()
except ValueError:
    events.append('outer')
assert events == ['inner', 'finally', 'outer']


def return_call_in_loop():
    for i in range(3):
        try:
            return boom()
        except ValueError:
            return f'caught in loop {i}'


assert return_call_in_loop() == 'caught in loop 0'