Exceptions
Monty implements a fixed set of exception classes, listed below. Sandboxed
code cannot define new exception classes: classes can inherit only from
classes defined in Python and object (see classes.md), so there
is no way to subclass BaseException/Exception. raise must therefore use one of
these built-ins; raise MyClass() on a plain user class raises
TypeError: exceptions must derive from BaseException, as in CPython.
BaseException, Exception, SystemExit, KeyboardInterrupt,
ArithmeticError, OverflowError, ZeroDivisionError, LookupError,
IndexError, KeyError, RuntimeError, NotImplementedError,
RecursionError, AttributeError, NameError,
UnboundLocalError, ValueError, UnicodeDecodeError, UnicodeEncodeError,
ImportError, ModuleNotFoundError, OSError, FileNotFoundError, FileExistsError,
IsADirectoryError, NotADirectoryError, PermissionError,
AssertionError, MemoryError, StopIteration, SyntaxError,
TimeoutError, TypeError.
Module-specific: json.JSONDecodeError and tomllib.TOMLDecodeError (subclasses of ValueError),
re.PatternError / re.error, io.UnsupportedOperation (catchable as
both OSError and ValueError, matching CPython’s dual parentage), and
dataclasses.FrozenInstanceError (a subclass of AttributeError).
Warning and all its subclasses (DeprecationWarning, etc.),
BufferError, EOFError, FloatingPointError, GeneratorExit,
ConnectionError and subclasses (ConnectionAbortedError,
ConnectionRefusedError, ConnectionResetError,
BrokenPipeError), BlockingIOError, ChildProcessError,
InterruptedError, ProcessLookupError, ReferenceError,
StopAsyncIteration, SystemError, TabError, IndentationError,
UnicodeError (parent), UnicodeTranslateError,
EncodingWarning, EnvironmentError / IOError aliases,
ExceptionGroup / BaseExceptionGroup (see language.md).
All exception constructors accept zero or one string argument only.
Multi-argument forms used in CPython (e.g. OSError(errno, strerror, filename),
UnicodeDecodeError(encoding, obj, start, end, reason)) are
not supported; passing more than one argument raises an internal error.
-
exc.args— a tuple with 0 or 1 elements. Always atuple, even when empty. -
str(exc)— returns the single message string, or""if none. -
KeyErroralways carries the missing key’sstr()text rather than the key itself, sostr(exc)andrepr(exc)quote non-string keys:{}[1]givesKeyError('1')where CPython givesKeyError(1), and abyteskey givesKeyError("b'a'"). The key’s__str__therefore runs when theKeyErroris raised, which CPython never does. An exception from it is replaced by the type name (KeyError('<K>')), but a resource limit hit inside it ends the run withMemoryErrororTimeoutErrorwhere CPython raises a plainKeyError. -
repr(exc)—ClassName('message')matching CPython, exceptUnicodeDecodeError/UnicodeEncodeError: CPython reprs these from their real 5-field constructor (UnicodeDecodeError('ascii', b'\xff', 0, 1, 'ordinal not in range(128)')), which Monty doesn’t track, so Monty’srepr()uses the generic single-message form instead. -
The dotted exception classes —
json.JSONDecodeError,tomllib.TOMLDecodeError,re.PatternError,binascii.Error,binascii.Incomplete,_csv.Error,statistics.StatisticsError— repr under their qualified name:binascii.Error('bad')where CPython givesError('bad').type(exc).__name__andstr(type(exc))match CPython. -
exc.__cause__,exc.__context__andexc.__suppress_context__are read-only:exc.__cause__ = otherraisesAttributeError, where CPython accepts an exception orNone. -
An exception rebuilt from a copy loses its chain: one that escapes a nested synchronous call (a
sorted()key,json.dumps(default=...), a__repr__) reaches the outer handler with__cause__reset toNone,__suppress_context__toFalse, and__context__set to the exception the caller was handling, if any, rather than the one the callee was handling. -
A chain that forms a cycle (
raise e from e) is only reclaimed when the session ends, since exceptions are not cycle-collected.
Not implemented: __traceback__, __notes__, add_note(). A traceback printed for an exception with a
__cause__ or __context__ shows that exception alone: CPython’s chained output (The above exception was the direct cause of the following exception:) is not produced, inside the sandbox or at the host boundary.
User class definitions are supported, but a class cannot derive from an
exception type (class Foo(Exception): raises TypeError: Monty does not support subclassing 'Exception': ...),
so there is no way to create a new exception class inside the sandbox. Raising a plain user class instance (raise MyClass()) fails
with TypeError: exceptions must derive from BaseException. Define custom
exception types on the host side if needed, or use the built-in subclass
that best fits.
break/continue/return inside a finally block follows CPython
semantics (the finally body runs exactly once and a return/break/
continue that exits it discards any in-flight exception), but Monty does
not emit CPython 3.14’s PEP 765 SyntaxWarning for such statements, having
no warnings machinery.
list.nonexistent raises AttributeError: type object 'list' has no attribute 'nonexistent', naming the class rather
than the metaclass, and calling it
(list.nonexistent()) reports the same message. Both use Monty’s name for the
class, which differs from CPython’s for one builtin: pathlib.Path reports
PosixPath, because Monty has a single type where CPython has the Path
class and its PosixPath instances (see pathlib.md).
Tracebacks are formatted to match CPython, including the
File "...", line N, in <function> lines and ~ caret markers (Monty
uses ~ where CPython uses ^; the test harness normalizes between
them). Frame names use <module> for top-level code.
Known caret divergences:
- CPython suppresses carets on a frame whose location is exactly the call in a
simple
name = f(...)assignment orreturn f(...)statement (a noise heuristic intraceback._should_show_carets); Monty always draws carets for the frame’s range. - For a frame whose location spans multiple lines (e.g. a caller frame covering
a whole multi-line
classstatement), Monty renders the CPython-style source block (all lines when the range covers at most three, otherwise...<N lines>...elision) but never draws caret markers under it, where CPython draws multi-line carets for partial-line ranges such as a multi-line binary expression.
Monty never emits CPython’s Did you mean: '...'? suggestions on
NameError/AttributeError. This divergence is invisible to the test
suite: scripts/run_traceback.py strips the suggestions from CPython’s output
before comparison, so traceback tests cannot catch it.