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
# mount-fs
import sys
from pathlib import Path

# root is injected by the test runner:
# - Monty: Path('/mnt') with OverlayMemory mount over a real temp directory
# - CPython: Path('<real_tmpdir>') pointing to a real temp directory

is_monty = sys.platform == 'monty'
is_windows = sys.platform == 'win32'

# ============================================================================
# TypeError — mkdir argument validation
# ============================================================================

# === TypeError on mkdir with too many positional arguments ===
# Both engines use pure-Python `def` range wording; the counts differ because
# CPython counts the bound `self` argument (`from 1 to 4 ... 5 were given`)
# while Monty binds only the visible parameters.
too_many_path = root / 'mkdir_too_many_args'
try:
    too_many_path.mkdir(0o777, False, False, 'extra')
    assert False, 'expected TypeError on mkdir too many positional args'
except TypeError as exc:
    if is_monty:
        assert str(exc) == 'Path.mkdir() takes from 0 to 3 positional arguments but 4 were given', (
            f'unexpected message: {exc}'
        )
    else:
        assert str(exc) == 'Path.mkdir() takes from 1 to 4 positional arguments but 5 were given', (
            f'unexpected message: {exc}'
        )
assert too_many_path.exists() == False

# === TypeError on mkdir with unknown keyword argument ===
unknown_kw_path = root / 'mkdir_unknown_kw'
try:
    unknown_kw_path.mkdir(bogus=True)
    assert False, 'expected TypeError on mkdir unknown kwarg'
except TypeError as exc:
    assert str(exc) == "Path.mkdir() got an unexpected keyword argument 'bogus'", f'unexpected message: {exc}'
assert unknown_kw_path.exists() == False

# === TypeError on mkdir with duplicate parents argument ===
duplicate_arg_path = root / 'mkdir_duplicate_arg'
try:
    duplicate_arg_path.mkdir(0o777, True, parents=False)
    assert False, 'expected TypeError on mkdir duplicate parents'
except TypeError as exc:
    assert str(exc) == "Path.mkdir() got multiple values for argument 'parents'", f'unexpected message: {exc}'
assert duplicate_arg_path.exists() == False

# ============================================================================
# FileNotFoundError — read/write/stat/unlink/rmdir on nonexistent paths
# ============================================================================

# === FileNotFoundError on read_text of nonexistent ===
try:
    (root / 'nonexistent.txt').read_text()
    assert False, 'expected FileNotFoundError on read_text'
except FileNotFoundError as exc:
    if is_monty:
        assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent.txt'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'

# === FileNotFoundError on read_bytes of nonexistent ===
try:
    (root / 'nonexistent.bin').read_bytes()
    assert False, 'expected FileNotFoundError on read_bytes'
except FileNotFoundError as exc:
    if is_monty:
        assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent.bin'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'

# === FileNotFoundError on unlink of nonexistent ===
try:
    (root / 'nonexistent.txt').unlink()
    assert False, 'expected FileNotFoundError on unlink'
except FileNotFoundError as exc:
    if is_monty:
        assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent.txt'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'

# === FileNotFoundError on stat of nonexistent ===
try:
    (root / 'nonexistent.txt').stat()
    assert False, 'expected FileNotFoundError on stat'
except FileNotFoundError as exc:
    if is_monty:
        assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent.txt'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'

# === FileNotFoundError on stat of deeply nonexistent ===
try:
    (root / 'nonexistent' / 'child.txt').stat()
    assert False, 'expected FileNotFoundError on stat'
except FileNotFoundError as exc:
    if is_monty:
        assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent/child.txt'", (
            f'unexpected message: {exc}'
        )
    elif not is_windows:
        assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'

# === FileNotFoundError on rmdir of nonexistent ===
try:
    (root / 'nonexistent_dir').rmdir()
    assert False, 'expected FileNotFoundError on rmdir nonexistent'
except FileNotFoundError as exc:
    if is_monty:
        assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent_dir'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'

# === FileNotFoundError on rename nonexistent ===
try:
    (root / 'nonexistent.txt').rename(root / 'new.txt')
    assert False, 'expected FileNotFoundError on rename nonexistent'
except FileNotFoundError as exc:
    if is_monty:
        assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent.txt'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'

# === Error on mkdir without parents when parent missing ===
try:
    (root / 'missing_parent' / 'child').mkdir()
    assert False, 'expected error on mkdir without parents'
except FileNotFoundError as exc:
    if is_monty:
        assert str(exc) == "[Errno 2] No such file or directory: '/mnt/missing_parent/child'", (
            f'unexpected message: {exc}'
        )
    elif not is_windows:
        assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'

# === FileNotFoundError on write_text with missing parent ===
try:
    (root / 'no_such_parent' / 'child.txt').write_text('should fail')
    assert False, 'expected FileNotFoundError on write_text with missing parent'
except FileNotFoundError as exc:
    if is_monty:
        assert str(exc) == "[Errno 2] No such file or directory: '/mnt/no_such_parent/child.txt'", (
            f'unexpected message: {exc}'
        )
    elif not is_windows:
        assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'

# === FileNotFoundError on write_bytes with missing parent ===
try:
    (root / 'no_such_parent' / 'child.bin').write_bytes(b'should fail')
    assert False, 'expected FileNotFoundError on write_bytes with missing parent'
except FileNotFoundError as exc:
    if is_monty:
        assert str(exc) == "[Errno 2] No such file or directory: '/mnt/no_such_parent/child.bin'", (
            f'unexpected message: {exc}'
        )
    elif not is_windows:
        assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'

# === FileNotFoundError on iterdir of nonexistent ===
try:
    list((root / 'nonexistent').iterdir())
    assert False, 'expected FileNotFoundError on iterdir nonexistent'
except FileNotFoundError as exc:
    if is_monty:
        assert str(exc) == "[Errno 2] No such file or directory: '/mnt/nonexistent'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 2] No such file or directory: '"), f'exc message: {exc}'

# ============================================================================
# FileExistsError — mkdir on existing
# ============================================================================

# === FileExistsError on mkdir of existing dir without exist_ok ===
try:
    (root / 'subdir').mkdir()
    assert False, 'expected FileExistsError on mkdir existing'
except FileExistsError as exc:
    if is_monty:
        assert str(exc) == "[Errno 17] File exists: '/mnt/subdir'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 17] File exists: '"), f'exc message: {exc}'

# === FileExistsError on mkdir(parents=True, exist_ok=False) of existing dir ===
try:
    (root / 'subdir').mkdir(parents=True, exist_ok=False)
    assert False, 'expected FileExistsError on mkdir parents=True exist_ok=False'
except FileExistsError as exc:
    if is_monty:
        assert str(exc) == "[Errno 17] File exists: '/mnt/subdir'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 17] File exists: '"), f'exc message: {exc}'

# === FileExistsError on mkdir(exist_ok=True) when path is a file ===
try:
    (root / 'hello.txt').mkdir(exist_ok=True)
    assert False, 'expected FileExistsError on mkdir exist_ok=True on a file'
except FileExistsError as exc:
    if is_monty:
        assert str(exc) == "[Errno 17] File exists: '/mnt/hello.txt'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 17] File exists: '"), f'exc message: {exc}'

# === OSError on rename directory onto non-empty directory ===
(root / 'rename_src_dir').mkdir()
(root / 'rename_src_dir' / 'moved.txt').write_text('moved')
(root / 'rename_dst_dir').mkdir()
(root / 'rename_dst_dir' / 'existing.txt').write_text('existing')
try:
    (root / 'rename_src_dir').rename(root / 'rename_dst_dir')
    assert False, 'expected OSError on rename dir onto non-empty dir'
except OSError as exc:
    if is_monty:
        assert str(exc) == "[Errno 39] Directory not empty: '/mnt/rename_dst_dir'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith(('[Errno 66] Directory not empty:', '[Errno 39] Directory not empty:')), (
            f'exc message: {exc}'
        )

# Rename onto empty directory should succeed (POSIX only — Windows rejects
# any rename where the destination already exists, even an empty directory).
if not is_windows:
    (root / 'rename_dst_empty').mkdir()
    (root / 'rename_src_dir').rename(root / 'rename_dst_empty')
    assert (root / 'rename_dst_empty' / 'moved.txt').read_text() == 'moved'
    assert not (root / 'rename_src_dir').exists(), 'source dir gone after rename'

# ============================================================================
# IsADirectoryError — read/write/unlink on directories
# ============================================================================

# === IsADirectoryError on read_text of directory ===
# CPython on Windows raises PermissionError instead of IsADirectoryError
try:
    (root / 'subdir').read_text()
    assert False, 'expected IsADirectoryError on read_text of dir'
except (IsADirectoryError, PermissionError) as exc:
    if is_monty:
        assert str(exc) == "[Errno 21] Is a directory: '/mnt/subdir'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 21] Is a directory: '"), f'exc message: {exc}'

# === IsADirectoryError on read_bytes of directory ===
try:
    (root / 'subdir').read_bytes()
    assert False, 'expected IsADirectoryError on read_bytes of dir'
except (IsADirectoryError, PermissionError) as exc:
    if is_monty:
        assert str(exc) == "[Errno 21] Is a directory: '/mnt/subdir'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 21] Is a directory: '"), f'exc message: {exc}'

# === IsADirectoryError on write_text to directory ===
try:
    (root / 'subdir').write_text('test')
    assert False, 'expected IsADirectoryError on write_text to dir'
except (IsADirectoryError, PermissionError) as exc:
    if is_monty:
        assert str(exc) == "[Errno 21] Is a directory: '/mnt/subdir'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 21] Is a directory: '"), f'exc message: {exc}'

# === IsADirectoryError on write_bytes to directory ===
try:
    (root / 'subdir').write_bytes(b'test')
    assert False, 'expected IsADirectoryError on write_bytes to dir'
except (IsADirectoryError, PermissionError) as exc:
    if is_monty:
        assert str(exc) == "[Errno 21] Is a directory: '/mnt/subdir'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 21] Is a directory: '"), f'exc message: {exc}'

# === IsADirectoryError/PermissionError on unlink of directory ===
# macOS returns PermissionError (EPERM), Linux returns IsADirectoryError (EISDIR)
try:
    (root / 'subdir').unlink()
    assert False, 'expected error on unlink of dir'
except (IsADirectoryError, PermissionError):
    pass

# ============================================================================
# NotADirectoryError — iterdir/rmdir on files
# ============================================================================

# === NotADirectoryError on iterdir of file ===
try:
    list((root / 'hello.txt').iterdir())
    assert False, 'expected NotADirectoryError on iterdir of file'
except NotADirectoryError as exc:
    if is_monty:
        assert str(exc) == "[Errno 20] Not a directory: '/mnt/hello.txt'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 20] Not a directory: '"), f'exc message: {exc}'

# === NotADirectoryError on rmdir of file ===
try:
    (root / 'hello.txt').rmdir()
    assert False, 'expected NotADirectoryError on rmdir of file'
except NotADirectoryError as exc:
    if is_monty:
        assert str(exc) == "[Errno 20] Not a directory: '/mnt/hello.txt'", f'unexpected message: {exc}'
    elif not is_windows:
        assert str(exc).startswith("[Errno 20] Not a directory: '"), f'exc message: {exc}'

# ============================================================================
# DirectoryNotEmpty — rmdir of non-empty directory
# ============================================================================

# === Error on rmdir of non-empty directory ===
try:
    (root / 'subdir').rmdir()
    assert False, 'expected error on rmdir non-empty'
except OSError as exc:
    if is_monty:
        assert str(exc) == "[Errno 39] Directory not empty: '/mnt/subdir'", f'unexpected message: {exc}'
    elif not is_windows:
        # macOS uses errno 66, Linux uses errno 39
        assert str(exc).startswith(('[Errno 66] Directory not empty:', '[Errno 39] Directory not empty:')), (
            f'exc message: {exc}'
        )

# ============================================================================
# UnicodeDecodeError — read_text of non-UTF-8 file
# ============================================================================

(root / 'bad_utf8.bin').write_bytes(b'\x80\x81\x82')
try:
    (root / 'bad_utf8.bin').read_text()
    assert False, 'expected UnicodeDecodeError on read_text of non-UTF-8'
except UnicodeDecodeError as exc:
    if is_monty:
        assert str(exc) == "'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte", (
            f'unexpected message: {exc}'
        )
    elif not is_windows:
        assert 'utf-8' in str(exc), f'exc message: {exc}'

# ============================================================================
# OSError — path length limits
# ============================================================================

# === OSError on path component too long (> 255 bytes) ===
long_name = 'a' * 256
long_component_path = Path(str(root) + '/' + long_name)
try:
    long_component_path.write_text('test')
    assert False, 'expected OSError on long component'
except OSError as exc:
    if not is_windows:
        assert str(exc).startswith(("[Errno 36] File name too long: '", "[Errno 63] File name too long: '")), (
            f'exc message: {exc}'
        )

# === Component at exactly 255 bytes is accepted ===
ok_name = 'b' * 255
assert Path(str(root) + '/' + ok_name).exists() == False

# === OSError on total path too long (> 4096 bytes) ===
long_path_str = str(root) + '/' + '/'.join(['x' * 200] * 21)
try:
    Path(long_path_str).write_text('test')
    assert False, 'expected OSError on long total path'
except OSError as exc:
    if not is_windows:
        assert str(exc).startswith(("[Errno 36] File name too long: '", "[Errno 63] File name too long: '")), (
            f'exc message: {exc}'
        )

# === OSError on long component in read operations too ===
try:
    long_component_path.read_text()
    assert False, 'expected OSError on read_text with long component'
except OSError as exc:
    if not is_windows:
        assert str(exc).startswith(("[Errno 36] File name too long: '", "[Errno 63] File name too long: '")), (
            f'exc message: {exc}'
        )

# === OSError on long component in stat ===
try:
    long_component_path.stat()
    assert False, 'expected OSError on stat with long component'
except OSError as exc:
    if not is_windows:
        assert str(exc).startswith(("[Errno 36] File name too long: '", "[Errno 63] File name too long: '")), (
            f'exc message: {exc}'
        )

# === OSError on long component in mkdir ===
try:
    long_component_path.mkdir()
    assert False, 'expected OSError on mkdir with long component'
except OSError as exc:
    if not is_windows:
        assert str(exc).startswith(("[Errno 36] File name too long: '", "[Errno 63] File name too long: '")), (
            f'exc message: {exc}'
        )

# ============================================================================
# TypeError — wrong argument types
# ============================================================================

try:
    (root / 'hello.txt').write_text(123)
    assert False, 'expected TypeError on write_text with int'
except TypeError as exc:
    assert str(exc) == 'data must be str, not int', f'unexpected message: {exc}'

try:
    (root / 'hello.txt').write_text()
    assert False, 'expected TypeError on write_text with no args'
except TypeError as exc:
    assert str(exc) == "Path.write_text() missing 1 required positional argument: 'data'", f'unexpected message: {exc}'

try:
    (root / 'hello.txt').write_bytes(123)
    assert False, 'expected TypeError on write_bytes with int'
except TypeError as exc:
    assert str(exc) == "memoryview: a bytes-like object is required, not 'int'", f'unexpected message: {exc}'

try:
    (root / 'hello.txt').write_bytes()
    assert False, 'expected TypeError on write_bytes with no args'
except TypeError as exc:
    assert str(exc) == "Path.write_bytes() missing 1 required positional argument: 'data'", f'unexpected message: {exc}'